如何在Mac OS X 10.10中设置GOPATH [英] How to set GOPATH in Mac OS X 10.10

查看:185
本文介绍了如何在Mac OS X 10.10中设置GOPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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: cassandra Dany $ export PATH = $ PATH:$ GOROOT / bin

Go安装在'/ usr /本地/去/斌/去。我将GOPATH设置为我的项目src目录。我能够在我的目录中运行代码。但是,当我尝试安装gocql时出现错误。

  Dineshs-MacBook-Air:go-cassandra Dany $ sudo go get github.com/gocql/gocql 
包github.com/gocql/gocql:无法下载,未设置$ GOPATH。有关更多详情,请参阅: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
密码:
package github .com / gocql / gocql:无法下载,$ GOPATH未设置。欲了解更多详情,请参阅: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 $


解决方案

注意:

GOROOT 应该引用一个文件夹(安装go的地方),而不是 go 可执行文件本身

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

正如戴夫在评论中提到的,你不应该有在你的情况下设置 GOROOT
查看文章你不需要设置 GOROOT ,真的是



GOPATH sh应该引用一个你可以找到 src pkg bin 。 (它不应直接引用 src 文件夹):

请参阅如何编写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 这里)



S ee sudo caveat


使用 sudo 命令调用这些位置时,不会反映添加到这些位置的任何变量,如 sudo 具有重置环境和设置安全路径的默认策略(此行为在 / etc / sudoers 中定义)


< blockquote>

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$

解决方案

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 (using export).
  • 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)

这篇关于如何在Mac OS X 10.10中设置GOPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆