我一直关注 http://tour.golang.org/ 直到我到达第三步,告诉你可以在你的系统上安装 gotour。 之后我用brew安装了go语言:
brew install hg
brew install go
然后我通过以下方式下载了 gotour:
go get code.google.com/p/go-tour/gotour
当我尝试启动 gotour 时,它无法识别该命令:
$ gotour
-bash: gotour: command not found
和
$ go gotour
和
$ ./gotour
所以我尝试查看路径,但它是空的,
echo $GOPATH
所以我定义了 GOPATH:
GOPATH=/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/
export GOPATH
现在我可以通过运行来运行 gotour
./gotour
但是我对我的 go 环境没有安全感..我不是应该能够运行 gotour 吗
go run gotour
或者只需输入(如本网站所述 http://www.moncefbelyamani.com/how-to-install-the-go-tour-on-your-mac/):
gotour
我想知道我是否以正确的方式做事,因为我是 Go 编程语言的新手。
mkdir $HOME/Go
mkdir -p $HOME/Go/src/github.com/user
export GOPATH=$HOME/Go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
brew install go
go get golang.org/x/tools/cmd/godoc
根据上面的混合答案,这就是我使用 Homebrew 对
OSX 10.12 (Sierra)
和 Go v1.7.1
起作用的方法:
我将 Kosh 的答案添加到我的
.zshrc
或 .bashrc
:
# Go development
export GOPATH="${HOME}/.go"
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
test -d "${GOPATH}" || mkdir "${GOPATH}"
test -d "${GOPATH}/src/github.com" || mkdir -p "${GOPATH}/src/github.com"
然后在新的终端窗口/选项卡中:
$ brew install go
==> Downloading https://homebrew.bintray.com/bottles/go-1.7.1.sierra.bottle.tar.gz
Already downloaded: /Users/nigel/Library/Caches/Homebrew/go-1.7.1.sierra.bottle.tar.gz
==> Pouring go-1.7.1.sierra.bottle.tar.gz
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
🍺 /usr/local/Cellar/go/1.7.1: 6,436 files, 250.6M
$ go get golang.org/x/tools/cmd/godoc
$ go get -u golang.org/x/lint/golint
$ go get golang.org/x/tour
$ tour
2016/10/19 12:06:54 Serving content from /Users/nigel/.go/src/golang.org/x/tour
2016/10/19 12:06:54 A browser window should open. If not, please visit http://127.0.0.1:3999
2016/10/19 12:06:55 accepting connection from: 127.0.0.1:52958
我想我已经找到了解决方案,我应该导出:
export PATH=$PATH:/usr/local/Cellar/go/1.0.2/bin/
而不是
GOPATH=/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/
export GOPATH
因为这就是“go get”放置二进制文件的地方(我猜)。 gotour 正在工作:
$ gotour
2012/10/11 18:35:50 Serving content from /usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/go-tour
2012/10/11 18:35:50 Open your web browser and visit http://127.0.0.1:3999/
顺便说一句,我的回答基于这篇文章
http://code.google.com/p/go-tour/issues/detail?id=39 他们谈论出口的地方:
/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/
Go 的入门页面: http://golang.org/doc/install 他们说你必须出口:
export PATH=$PATH:/usr/local/go/bin
我把它放在我的 ${HOME}/.bash_profile 中
export GOPATH="${HOME}/.go"
export GOROOT="$(brew --prefix golang)/libexec"
export PATH="$PATH:${GOPATH}/bin:${GOROOT}/bin"
test -d "${GOPATH}" || mkdir "${GOPATH}"
test -d "${GOPATH}/src/github.com" || mkdir -p "${GOPATH}/src/github.com"
基于 Golfadas 答案,但已更新以适用于新旧版本的 brew。
我认为你应该先设置 GOPATH
go get
。
好吧,至少在我的机器上这工作顺利。
我将 GOPATH 设置为我的主文件夹中的一个文件夹。
希望这有帮助!
这里只是更新 - 我遇到了同样的问题,之前的答案没有帮助。在 Homebrew 安装的当前(~1.2.2)版本的 Go 上,您必须将 GOROOT 设置为以下内容:
export GOROOT=/usr/local/Cellar/go/1.2.2/libexec
我对 go 文件夹结构有点不熟悉,或者通过自制程序对 go 安装进行更改,所以我真的不知道为什么。但是 - 如果您缺少似乎所有核心包的内容,上面的内容应该可以修复。