英文:
How to set GOPATH in Mac OS X 10.10
问题
我在Mac OS X上安装了Go 1.4。之前我有Go 1.0版本。我将GOROOT和PATH设置如下:
Dineshs-MacBook-Air:go-cassandra Dany$ which go
/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Go安装在'/usr/local/go/bin/go'目录下。我将GOPATH设置为我的项目源代码目录。我可以在我的目录中运行Go代码。但是当我尝试安装gocql时,出现错误。
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
有人可以帮我解决这个问题吗?谢谢。
编辑1: @VonC 我也尝试了另一种选项。我将GOROOT更改为Go安装的目录,但没有帮助。我还更改了GOPATH。
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Dineshs-MacBook-Air:go-cassandra Dany$ export GOPATH=/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
Password:
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Dineshs-MacBook-Air:go-cassandra Dany$ echo $GOPATH
/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ ls
bin pkg src
Dineshs-MacBook-Air:go-cassandra Dany$
英文:
I installed Go 1.4 in Mac OS X. Previously I had Go 1.0. I set the GOROOT and PATH as follows,
Dineshs-MacBook-Air:go-cassandra Dany$ which go
/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go/bin/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Go is installed in '/usr/local/go/bin/go'. And I set the GOPATH as my project src directory. I am able to run go code inside my directory. But when I try to install gocql I am getting error.
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Could anyone help me on this? Thank you
EDIT 1: @VonC I tried the other option as well. I changed the GOROOT to the directory where go is installed. But it didn't help. And I changed the GOPATH.
Dineshs-MacBook-Air:go-cassandra Dany$ export GOROOT=/usr/local/go
Dineshs-MacBook-Air:go-cassandra Dany$ export PATH=$PATH:$GOROOT/bin
Dineshs-MacBook-Air:go-cassandra Dany$ export GOPATH=/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ sudo go get github.com/gocql/gocql
Password:
package github.com/gocql/gocql: cannot download, $GOPATH not set. For more details see: go help gopath
Dineshs-MacBook-Air:go-cassandra Dany$ echo $GOPATH
/Users/Dany/Documents/FALL-2013-COURSES/Imp_Data_structures/workspace/go-cassandra
Dineshs-MacBook-Air:go-cassandra Dany$ ls
bin pkg src
Dineshs-MacBook-Air:go-cassandra Dany$
答案1
得分: 58
注释:
GOROOT
应该引用一个文件夹(go 安装的位置),而不是 go
可执行文件本身。
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
正如 Dave 在评论中提到的,你在这种情况下根本不需要设置 GOROOT
。请参阅文章 You don’t need to set GOROOT
, really。
GOPATH
应该引用一个文件夹,在该文件夹下你会找到 src
、pkg
和 bin
。(它不应该直接引用 src
文件夹):
请参阅“How to Write Go Code - Workspace”。
关于 GOPATH
:
- 尝试在你的
~/.bashrc
中设置它(使用export
)。 - 检查你当前的 shell 是否是 bash(而不是其他的,比如
fish
)。 - 检查
go env
的输出。
不要使用 sudo go get
,因为用于 sudo
的环境变量(root
)与当前用户不同:
go get github.com/gocql/gocql
(或者 你需要执行 sudo -E bash -c 'go get github.com/gocql/gocql'
,但我怀疑你在这里不需要 root
)。
请参阅 sudo caveat:
> 当使用 sudo
命令时,这些位置添加的任何变量都不会被反映出来,因为 sudo
有一个重置环境并设置安全路径的默认策略(此行为在 /etc/sudoers
中定义)。
英文:
Notes:
GOROOT
should reference a folder (where go is installed), not the go
executable itself
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
As Dave mentions in the comments, you should not have to set GOROOT
at all in your case.
See the article You don’t need to set GOROOT
, really.
GOPATH
should reference a folder under which you will find src
, pkg
and bin
. (it should not reference directly the src
folder):
See "How to Write Go Code - Workspace"
Regarding the GOPATH
:
- try and set it in your
~/.bashrc
(usingexport
). - check that your current shell is a bash (and not another one like
fish
) - check the output of
go env
.
Don't do a sudo go get
, as the environment variable used for sudo
(root
) wouldn't be the same as the current user:
go get github.com/gocql/gocql
(or you would need to do a sudo -E bash -c 'go get github.com/gocql/gocql'
, but I suspect you don't need root
here)
See sudo caveat:
> Any variables added to these locations will not be reflected when invoking them with a sudo
command, as sudo
has a default policy of resetting the Environment and setting a secure path (this behavior is defined in /etc/sudoers
)
答案2
得分: 6
你需要告诉Go你的工作空间的位置。在这个例子中,我们将使用$HOME/dev/go-workspace。
然后,你需要知道你的Mac是否配置了zsh或bash作为shell。
> 文件~/.zshrc用于zsh shell。zsh shell是在Big Sur操作系统中引入的。
>
> 文件~/.bashrc是bash shell,在之前的操作系统版本中使用的,对于Linux用户也是如此。
1:根据你的shell,在你的~/.zsh或~./bashrc中添加以下行来导出所需的变量。
对于从https://golang.org/doc/install下载的原始pkg安装的Go:
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
对于从Homebrew安装的Go(运行brew update和brew install golang):
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
2:运行以下命令:
# source ~/.zshrc
或者
source ~./bashrc
根据在步骤#2中插入的新变量更新你的$PATH。
3:然后创建你的工作空间目录:
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
4:创建一个test.go文件,内容为hello world:
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
通过执行以下命令来运行你的程序:
$ go run test.go
如果你希望将其编译并移动到$GOPATH/bin,然后运行:
$ go install test.go
由于我们已经将$GOPATH/bin添加到你的$PATH中,你可以在任何地方运行你的程序,只需键入test:
$ test
如果一切正常,输出将是:
hello, world
英文:
You will need to inform to Go the location of your workspace. In this example we gonna use $HOME/dev/go-workspace.
Then you need to know if your mac have zsh or bash configured as shell.
> The file ~/.zshrc is used for zsh shell. The zsh shell was introduced
> in OS Big Sur.
>
> The ~/.bashrc is the bash shell used in previews OS version, the same for linux users.
1: Add those lines to export the required variables in your ~/.zsh or ~./bashrc depending on your shell.
For go installed from original pkg download from https://golang.org/doc/install
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/go
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
For Go installed from home brew. (brew update and brew install golang)
export GOPATH=$HOME/dev/go-workspace
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
2: run
# source ~/.zshrc
or
source ~./bashrc
to update your $PATH according with the new variables inserted in step #2
3: Then create your the workspace directories:
$ mkdir -p $GOPATH $GOPATH/src $GOPATH/pkg $GOPATH/bin
4: create a test.go, the hello world
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Run your program by executing:
$ go run test.go
If you wish to compile it and move it to $GOPATH/bin, then run:
$ go install test.go
Since we have $GOPATH/bin added to your $PATH, you can run your program from anywhere just typing test :
$ test
If everything is right the output will be :
hello, world
答案3
得分: 2
tldr;
macOS终端:
brew install go
编辑~/.zshenv
export GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
export GOPATH="/Users/Shared/Development/go-workspace"
export PATH="$PATH:$GOPATH/bin"
将GOPATH视为环境配置的工作空间。GOROOT是go语言的安装点。Homebrew将bin命令符号链接到/usr/local/bin。将**$GOPATH添加到PATH**中,以便找到稍后安装的任何go程序。
英文:
tldr;
macos terminal:
brew install go
edit ~/.zshenv
export GOROOT="/usr/local/Cellar/go/1.16.6/libexec"
export GOPATH="/Users/Shared/Development/go-workspace"
export PATH="$PATH:$GOPATH/bin"
Think of the GOPATH in terms of a configured workspace for the environment. GOROOT is the install point for the go language. Homebrew will symlink the bin commands to your /usr/local/bin. Add the $GOPATH to your PATH in order to find any go programs that you later install.
答案4
得分: 0
我在MacOS上设置GOROOT和GOPATH时遇到了很多问题,唯一有效的解决方法如下:
将以下代码添加到你的~/.bashrc
或~/.profile
文件中,如果你使用Oh My Zsh,则添加到~/.zshrc
文件中:
export GOPATH=$HOME/go
export GOROOT=/usr/local/go
然后,在终端中运行以下命令:
source ~/.profile
如果你使用的是其他不同的shell,请将.profile
替换为相应的文件名。
信息: $HOME
变量指向当前用户的目录。
信息2: /usr/local/go
是所有用户的go文件和bin文件夹所在的位置。
设置自定义GOPATH作为工作目录:
如果你想将GoLang项目存储在不同的文件夹中,你可以设置自定义路径。这是我的自定义GOPATH,它指向我的admin/Documents文件夹中的goWorkSpace文件夹:
export GOPATH=$HOME/Documents/goWorkSpace
英文:
I face many issues to set the GOROOT and GOPATH in MacOS, and the only solution that worked for me, is the below one:
Add the following code into your ~/.bashrc
or ~/.profile
if you use Oh My Zsh ~/.zshrc
export GOPATH=$HOME/go
export GOROOT=/usr/local/go
After that, run the following command in your terminal:
source ~/.profile
If you have any different shell, replace it in the place of .profile
.
Info: $HOME
variable will point to your current user's directory.
Info 2: /usr/local/go
is where all users, the go files, and bin folder exists.
To Set Custom GOPATH as working directory:
To store your projects of GoLang in different folders, you can set the custom path. Here is my custom GOPATH, which points to my goWorkSpace folder in my admin/Documents folder.
export GOPATH=$HOME/Documents/goWorkSpace
答案5
得分: 0
你的 $PATH
设置不正确。在安装过程中,$GOROOT
被设置并添加到了 $PATH
中。因此,以下两种设置都是不需要的:
export GOROOT=/usr/local/go/bin/go
export PATH=$PATH:$GOROOT/bin
或者:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
因为根据你安装 Go 的方式不同(Homebrew、go.dev 等),$GOROOT
的值也会不同。
你只需要设置 $GOPATH
。
如果你觉得 XDG Base Directory Specification 有用(这样可以保持你的 $HOME
目录整洁),你可以按照以下步骤操作:
-
创建一个用户特定的数据目录
mkdir -p $HOME/.local/share/go
-
在
.zshenv
中设置$GOPATH
export GOPATH="$HOME/.local/share/go" export PATH="$PATH:$GOPATH/bin"
-
刷新环境变量
source .zshenv # 或者退出并重新打开 shell
-
安装 Go 包
go install golang.org/x/tools/gopls@latest
-
验证包的版本
which gopls
英文:
You're setting $PATH
incorrectly. $GOROOT
is set and added to the $PATH
during installation. So neither:
export GOROOT=/usr/local/go/bin/go
export PATH=$PATH:$GOROOT/bin
nor:
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
is needed because $GOROOT
will be different based on how you've installed Go - Homebrew, go.dev, etc.
You just need to set $GOPATH
.
If you find XDG Base Directory Specification useful (this will keep your $HOME
clean), you could:
-
Create a user-specific data directory
mkdir -p $HOME/.local/share/go
-
Set
$GOPATH
in.zshenv
export GOPATH="$HOME/.local/share/go" export PATH="$PATH:$GOPATH/bin"
-
Refresh environment variables
source .zshenv # or quit and re-open the shell
-
Install Go package
go install golang.org/x/tools/gopls@latest
-
Verify version of package
which gopls
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论