使用brew安装go,并运行gotour。

huangapple go评论85阅读模式
英文:

Install go with brew, and running the gotour

问题

我一直在跟着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

所以我尝试查看go路径,但它是空的,

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编程语言还不熟悉。

英文:

I was following the http://tour.golang.org/ untill I got to the third step about that tells you that you can install the gotour on your system.
After that I've installed the go language with brew by:

brew install hg
brew install go

Then I downloaded the gotour by:

go get code.google.com/p/go-tour/gotour

When I tried to launch the gotour it didnt recognise the command:

$ gotour
-bash: gotour: command not found

and

$ go gotour

and

$ ./gotour

So I tried to see the go path and it was empty,

echo $GOPATH

so I defined the GOPATH:

GOPATH=/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/
export GOPATH

Now I can run the gotour by runing

./gotour

But I'm insecure about my go enviroment.. wasn't I suposed to be able to run gotour by

go run gotour

or just by typing (like is described on this website http://www.moncefbelyamani.com/how-to-install-the-go-tour-on-your-mac/):

gotour

I would like to know if i'm doing things the right way since I'm new to the go programing language.

答案1

得分: 192

在OSX上使用homebrew安装go 1.4:

1) 创建目录

mkdir $HOME/Go
mkdir -p $HOME/Go/src/github.com/user

2) 设置路径

export GOPATH=$HOME/Go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

3) 安装Go

brew install go

4) 使用"go get"获取基础工具

go get golang.org/x/tools/cmd/godoc

5) 从这里开始:https://golang.org/doc/code.html,从"your first program"开始。

英文:

Installing go 1.4 with homebrew on OSX:

1) Create Directories

mkdir $HOME/Go
mkdir -p $HOME/Go/src/github.com/user

2) Setup your paths

export GOPATH=$HOME/Go
export GOROOT=/usr/local/opt/go/libexec
export PATH=$PATH:$GOPATH/bin
export PATH=$PATH:$GOROOT/bin

3) Install Go

brew install go

4) "go get" the basics

go get golang.org/x/tools/cmd/godoc

5) Start here: https://golang.org/doc/code.html at "your first program"

答案2

得分: 57

根据上面的一些答案,这是在OSX 10.12 (Sierra)Go v1.7.1上使用Homebrew适用于我的方法:

我将以下内容添加到我的.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 github.com/golang/lint/golint

$ go get golang.org/x/tour/gotour

$ gotour
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
英文:

Following a mix of answers above, this is what worked for me on OSX 10.12 (Sierra) and Go v1.7.1 using Homebrew:

I added this from Kosh's answer to my .zshrc or .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"

Then in a new terminal window/tab:

$ 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 github.com/golang/lint/golint

$ go get golang.org/x/tour/gotour

$ gotour
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

答案3

得分: 23

我认为我已经找到了解决方案,我应该导出:

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 从/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/go-tour提供内容
2012/10/11 18:35:50 打开您的Web浏览器并访问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
英文:

I think I have found the solution, I should have exported:

export PATH=$PATH:/usr/local/Cellar/go/1.0.2/bin/

Instead of

GOPATH=/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/
export GOPATH

Since thats where 'go get' puts the binaries (I guess). gotour is working:

$ 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/ 

Btw I based my answer on this post

http://code.google.com/p/go-tour/issues/detail?id=39
where they talk about exporting:

/usr/local/Cellar/go/1.0.2/src/pkg/code.google.com/p/

And the getting started page from go:
http://golang.org/doc/install
where they say you have to export:

export PATH=$PATH:/usr/local/go/bin

答案4

得分: 9

我将这个放在我的${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。

英文:

I put this in my ${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"

based on golfadas answer but updated to work with old and new versions of brew.

答案5

得分: 8

我认为你在执行go get之前应该设置好GOPATH。至少在我的机器上,这样做是顺利的。

我将GOPATH设置为了我的主文件夹中的一个文件夹。

希望这能帮到你!

英文:

I thing you should have set the GOPATH before you go get.
Well, at least here in my machine this worked smoothly.

I set the GOPATH to a folder in my home folder.

Hope this helps!

答案6

得分: 5

只是一个更新 -
我遇到了同样的问题,之前的答案没有帮助。在当前(~1.2.2)通过Homebrew安装的Go版本上,你必须将GOROOT设置为以下内容:

export GOROOT=/usr/local/Cellar/go/1.2.2/libexec

我对go文件夹结构或通过homebrew安装go的更改不太熟悉,所以我不太清楚为什么。但是 - 如果你缺少所有核心包,上述方法应该可以解决问题。

英文:

Just an update here -
I ran into this same problem, and the previous answers did NOT help. On current (~1.2.2) versions of Go installed by Homebrew, you have to set GOROOT to the following:

export GOROOT=/usr/local/Cellar/go/1.2.2/libexec

I'm a little unfamiliar with the go folder structure, or changes to the go installation via homebrew, so I don't really know why. But - if you're missing what seems like all the core packages, the above should fix.

huangapple
  • 本文由 发表于 2012年10月11日 23:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/12843063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定