Go构建:无法找到包,尽管GOPATH和模块已配置
大家好!我是Go新手,已经第二天在解决这个问题了。我写了一个简单的程序,使用外部包处理JSON(例如`github.com/tidwall/gjson`)。我在Ubuntu上安装了Go 1.22,创建了文件夹`~/go_projects/myapp`,里面有一个`main.go`文件。
这是我的代码:
```go
package main
import (
"fmt"
"github.com/tidwall/gjson"
)
func main() {
json := `{"name": "Alice", "age": 30}`
value := gjson.Get(json, "name")
fmt.Println(value.String())
}
```
我执行了`export GOPATH=$HOME/go`和`export PATH=$PATH:$GOPATH/bin`,然后在项目文件夹中运行了`go mod init myapp`。之后尝试`go build`,但得到错误:
```
build command-line-arguments: cannot find package "github.com/tidwall/gjson" in any of:
/usr/local/go/src/github.com/tidwall/gjson (from $GOROOT)
/home/user/go/src/github.com/tidwall/gjson (from $GOPATH)
```
我试过`go get github.com/tidwall/gjson`——它提示该命令已过时。