英文:
Cannot set $GOPATH on Mac OSX
问题
我正在尝试将我的$GOPATH
变量设置为在我的机器上运行一些示例代码:
$ smitego-example go run main.go
main.go:5:2: 找不到包“github.com/#GITHUB_USERNAME#/smitego”的任何一个:
/usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego(来自$GOROOT)
($GOPATH未设置)
$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': 不是有效的标识符
github.com/#GITHUB_USERNAME#/smitego/smitego.go
的内容:
package smitego
我该如何设置我的GOPATH
,以便它始终有效?
英文:
I'm trying to set my $GOPATH
variable to run some example code on my machine:
$ smitego-example go run main.go
main.go:5:2: cannot find package "github.com/#GITHUB_USERNAME#/smitego" in any of:
/usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego (from $GOROOT)
($GOPATH not set)
$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': not a valid identifier
Contents of github.com/#GITHUB_USERNAME#/smitego/smitego.go
:
package smitego
How can I set my GOPATH
so it works always and forever?
答案1
得分: 147
更新,截至Go 1.8版本:如果你正在安装Go 1.8(发布于2017年2月)或更新版本,GOPATH将由Go工具链自动确定。在macOS(之前的OS X)上,默认为$HOME/go
,例如/Users/matt/go/
。这使得开始使用Go变得更加容易,你可以在安装Go后立即使用go get <package>
。
对于shell环境:(手动方法)
~/.bash_profile文件应包含export GOPATH=$HOME/go
和export PATH=$GOPATH/bin:$PATH
。使用$
符号是重要的:请确保注意我使用了$
符号的位置(以及我没有使用的位置)。
对于Sublime Text:
Sublime Text菜单 > Preferences > Package Settings > GoSublime > Settings: User
{
"shell": ["/bin/bash"],
"env": {"GOPATH": "/Users/#USERNAME#/go/"},
}
确保你的GOPATH
没有设置为包的完整路径,只需设置为你的go
文件夹的根目录,其中包含src、pkg和bin
。如果你没有使用GoSublime,我建议你先安装它。
英文:
Update, as of Go 1.8: If you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you.
It defaults to $HOME/go
on macOS (nee OS X) - e.g. /Users/matt/go/
. This makes getting started with Go even easier, and you can go get <package>
right after installing Go.
For the shell: (the manual method)
~/.bash_profile should contain export GOPATH=$HOME/go
and also export PATH=$GOPATH/bin:$PATH
. The use of the $
is important: make sure to note where I've used it (and where I have not).
For Sublime Text:
Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User
{
"shell": ["/bin/bash"],
"env": {"GOPATH": "/Users/#USERNAME#/go/"},
}
Make sure your GOPATH
is not set to the full path of the package; just the root of your go
folder where src, pkg, and bin
reside. If you're not using GoSublime, I'd suggest installing that first.
答案2
得分: 54
接受的答案对我没有起作用。我进行了调查并找到了原因:我正在使用zsh而不是bash。
我需要在~/.zshrc
中添加以下两行:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
英文:
The accepted answer didn't work for me. I investigated and found the cause: I am using zsh, not bash.
I need to add the following two lines to ~/.zshrc
:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
答案3
得分: 18
-
下载并安装Go工具
https://golang.org/doc/install -
设置Go工作空间
mkdir $HOME/go && cd $HOME/go
mkdir bin pkg src
-
设置Go环境
sudo vi ~/.bash_profile
export GOPATH=$HOME/go
PATH=$PATH:$GOPATH/bin
通过创建、构建和运行一个Go项目进行测试
mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello
英文:
-
Download and install Go tools
https://golang.org/doc/install -
Setup Go workspace
mkdir $HOME/go && cd $HOME/go
mkdir bin pkg src
-
Setup Go environment
sudo vi ~/.bash_profile
export GOPATH=$HOME/go
PATH=$PATH:$GOPATH/bin
Test by creating, building and running a Go project
mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello
答案4
得分: 17
当你赋值变量时,不需要在变量前加上$
前缀,只有在读取变量时才需要。
export GOPATH=$HOME
要使其永久生效,请将该命令放入你的.bash_profile
文件中。
这将适用于终端shell。如果你需要设置会影响GUI应用程序的环境变量,请参考https://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x/4567308#4567308。
英文:
You don't put the $
prefix on a variable when you're assigning it, only when you're reading it.
export GOPATH=$HOME
To make this permanent, put the command in your .bash_profile
.
That will work for Terminal shells. If you need to set environment variables that will affect GUI applications, see https://stackoverflow.com/questions/603785/environment-variables-in-mac-os-x/4567308#4567308
答案5
得分: 5
http://www.golang-book.com/guides/machine_setup#osx只提供了在~/.bashrc
中设置路径的说明,而没有提到~/.bash_profile
。感谢这个帖子,我才能够成功构建我的示例文件。
export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin
其他Mac用户需要将上述内容添加到他们的~/.bash_profile
中。
英文:
The http://www.golang-book.com/guides/machine_setup#osx
only has instructions for setting the path on ~/.bashrc
, not ~/.bash_profile
which thanks to this thread was able to get my example file to build.
export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin
Other Mac users need to add the above to their ~/.bash_profile
.
答案6
得分: 5
安装Go后,可以使用brew或者包管理器来解决我的问题:
export GOROOT="/usr/local/go"
export GOPATH="$HOME/Documents/goWorkSpace"
export PATH="$HOME/Documents/goWorkSpace/bin:$PATH"
英文:
After installing go with brew or with package this solved my problem:
export GOROOT="/usr/local/go"
export GOPATH="$HOME/Documents/goWorkSpace"
export PATH="$HOME/Documents/goWorkSpace/bin:$PATH"
答案7
得分: 1
在 macOS High Sierra 版本 10.3.3 上,已安装 Go,安装位置如下:
在 ~/.bashrc 中添加以下内容:
export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
然后 Go 就可以正常工作了。
英文:
on macOS High Sierra Version 10.3.3, Go Installed here :
Added following on :~/.bashrc
export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
and then Go Works
答案8
得分: -1
使用最新的Mac和Catalina版本的人们,你们需要更新.zshrc文件而不是.bash。
将以下两行添加到~/.zshrc文件中:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
应该可以正常工作!!
这个改变是一段时间前发生的,请参考下面的链接了解为什么使用.zshrc而不是.bash_profile
https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/
英文:
People working with the latest macs and above Catalina version,
you guys need to update the .zshrc file instead of .bash.
Add the following two lines to ~/.zshrc:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
it should work.!!
This got change a while back, please refer to the link below to understand why .zshrc and not .bash_profile
https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论