问题描述
我正在尝试设置我的 $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
github.com/#GITHUB_USERNAME#/smitego/smitego.go的内容
:
package smitego
我怎样才能设置我的 GOPATH
让它永远有效?
How can I set my GOPATH
so it works always and forever?
推荐答案
从 Go 1.8 开始更新:如果您安装的是 Go 1.8(发布时间:2017 年 2 月)或更高版本,则 GOPATH 为 由 Go 工具链为您自动确定.
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.
它在 macOS (nee OS X) 上默认为 $HOME/go
- 例如/Users/matt/go/
.这使得 Go 入门变得更加容易,您可以在安装 Go 后立即go get <package>
.
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.
对于外壳:(手动方法)
~/.bash_profile 应该包含 export GOPATH=$HOME/go
和 export PATH=$GOPATH/bin:$PATH
.$
的使用很重要:请务必记下我在哪里使用过它(以及我没有在哪里使用过).
~/.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).
对于崇高文本:
Sublime Text 菜单 > 首选项 > 包设置 > GoSublime > 设置:用户
Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User
{
"shell": ["/bin/bash"],
"env": {"GOPATH": "/Users/#USERNAME#/go/"},
}
确保您的 GOPATH
未设置为包的完整路径;只是 src、pkg 和 bin
所在的 go
文件夹的根目录.如果您不使用 GoSublime,我建议您先安装它.
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.
这篇关于无法在 Mac OSX 上设置 $GOPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!