~/.bashrc 上的问题,写入的文件路径不正确

Issue on ~/.bashrc, written file PATH is not correct

我正在 Ubuntu 16.04 计算机上下载 Go。我正在学习本教程,但我无法在 youtube 上从这部分取得进展 https://www.youtube.com/watch?v=YS4e4q9oBaU&t=1810s:

当我创建 Main.go VSC 控制台时出现错误:

Failed to run '/usr/local/go/bin/go env' (cwd:/home/santiagoquinteros/code): 
Error: Command failed:/usr/local/go/bin/go env GOPATH GOROOT GOPROXY GOBIN GOMODCACHE
missing $GOPATH

如果我在控制台上写这个命令,这会显示:

~$ go get github.com/nsf/gocode
missing $GOPATH

这是我的 ~/.bashrc 中的内容:

export GOROOT=/usr/local/go 
export PATH=$PATH:/usr/local/go/bin
export GOPATH=/home/santiagoquinteros/golib
export PATH=$PATH:$GOPATH/bin
export GOPATH=$gopath:/home/santiagoquinteros/code

在从 19:10 到 25:15 的视频中,创建了这些代码行,我没有做任何其他事情就跟着他,但出于某种原因,VSC 正在将自己重定向到另一个我没有的 PATH提到。我也在正确的目录中创建了两个名为“golib”和“code”的文件夹。

我也在这些页面上搜索过,但他们的所有解决方案都没有帮助我:

https://golang.org/doc/install
https://gist.github.com/vsouza/77e6b20520d07652ed7d
https://github.com/microsoft/vscode/issues/108003
https://github.com/microsoft/vscode-go/issues/2595
https://askubuntu.com/questions/1238895/go-cannot-find-goroot-directory-error-on-installing-go-in-wsl

请注意,我正在开发 ubuntu 16.04 的用户限制版本,这可能是主要问题。

VSC 代表 VisualStudioCode

我猜错误消息来自 https://github.com/golang/go/blob/release-branch.go1.16/src/cmd/go/internal/modload/init.go#L207-L210

在你的 ~/.bashrc 的最后一行,你的意思是 export GOPATH=$GOPATH:...,而不是 export GOPATH=$gopath:...。由于未设置 $gopath,您的 GOPATH 最终将成为 :/home/santiagoquinteros/code。上面的代码不喜欢。

正如其他人所说,您不需要设置GOROOT。使用 go1.16,模块模式是默认的,所以你可能不需要设置 GOPATH 但可以使用默认的 GOPATH$HOME/go.

最近发生了很多变化。我推荐较新的材料集,例如 https://golang.org/doc/#getting-started, https://learn.go.dev, https://play-with-go.dev/ , ...