找不到 GOPATH 甚至设置 env 路径

Can't find GOPATH even set env path

使用安装的 root 用户继续 linux。

在 ~/.zshrc 文件中设置 go 路径:

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
$ ls $HOME/go
bin pkg src

$ ls $HOME/go/bin
asmfmt  dlv  errcheck  fillstruct  gocode  gocode-gomod  godef  gogetdoc  goimports  golint  gometalinter  gomodifytags  gorename  gotags  gounit  guru  iferr  impl  keyify  motion

但是 go env 返回:

zsh: command not found: go

为什么?


加法

$ whereis go
go: /usr/local/go

/usr/local设置为~/.zshrc:

export PATH=$PATH:/usr/local/

获取它。 运行 go env 返回:

zsh: permission denied: go

如评论所述,您的 ~/.zshrc 应将 PATH 设置为引用 Go,如 Go wiki:

中所示

Edit your ~/.zshrc file to add the following line:

export GOPATH=$HOME/go

Save and exit your editor. Then, source your ~/.zshrc.

source ~/.zshrc

default GOPATH and issue 17262 中所述,您无需设置 GOPATH。

但是自从 Go 1.11 and its modules 以来,您现在可以创建整个项目 而无需 完全使用默认值 GOPATH:一切都将保留在您的项目本地。

As of Go 1.11, the go command enables the use of modules when the current directory or any parent directory has a go.mod, provided the directory is outside $GOPATH/src.
(Inside $GOPATH/src, for compatibility, the go command still runs in the old GOPATH mode, even if a go.mod is found)