我已经看到可能重复的问题,但他们似乎建议使用 go 模块。但我想知道为什么GOPATH 有据可查的功能不能开箱即用:-
导入路径 P 表示在 GOPATH 环境变量中列出的某些 DIR 目录 DIR/src/P 中找到的包(有关更多详细信息,请参阅:'go help gopath')。
我正在尝试使用文件 main.go 中的 root.go
~/src/github.com/himanshugarg/sicp/root$ ls
main.go root.go test.go
文件 root.go 具有包声明:-
~/src/github.com/himanshugarg/sicp/root$ head root.go
// Find square root using Newton's method
// Based on SICP's on page 30
package root
import (
"math"
);
func Sqrt(x float64) float64{
goodEnough := func (guess float64) bool {
文件 main.go 导入根目录:-
~/src/github.com/himanshugarg/sicp/root$ head main.go
package main
import (
"fmt"
"os"
"strconv"
"github.com/himanshugarg/sicp/root"
);
func main() {
GOPATH 设置正确:-
~/src/github.com/himanshugarg/sicp/root$ echo $GOPATH
/home/hgarg
但构建失败: -
~/src/github.com/himanshugarg/sicp/root$ go build main.go
main.go:7:3: no required module provides package github.com/himanshugarg/sicp/root: go.mod file not found in current directory or any parent directory; see 'go help modules'
当然,我可以使用 go 模块,但我只是想确保 GOPATH 不再有效,或者我没有遗漏任何东西。
守候你守候我
相关分类