在Visual Studio Code 下运行go程序,报错如下
cannot find package "github.com/gin-gonic/gin" in any of
C:\Go\src\github.com\gin-gonic\gin (from $GOROOT)
C:\Gocode\src\github.com\gin-gonic\gin (from $GOPATH)
造成的原因是:在使用 GOPROXY 的时候,开启了 GO111MODULE,导致包管理非官方所说的在 $GOPATH\src\,而是去了 $GOPATH\pkg\目录下
处理方法:运行前先运行以下两条命令,再go run xxx就可以了。
go mod init gin
go mod edit -require github.com/gin-gonic/gin@latest
一定要在visual studio code终端下执行