英文:
Go : go get $GOPATH error, when GOPATH is set
问题
当运行go get命令时:
sudo go get github.com/go-sql-driver/mysql
我得到以下错误:
package github.com/go-sql-driver/mysql: 无法下载,$GOPATH未设置。有关详细信息,请参阅:go help gopath
然而,$GOPATH已经设置。
运行echo $GOPATH
会得到/Users/userxyz/Desktop/Code
运行go env
会得到
......
GOPATH="/Users/userxyz/Desktop/Code"
...
GOROOT="/usr/local/go"
.....
我已经尝试通过添加以下行将GOPATH设置为环境变量:
export GOPATH="$HOME/Desktop/Code"
export PATH=$PATH:$GOPATH/bin
分别添加到以下文件中:
~/.profile (/etc/profile)
~/.bashrc
~/.bash_profile
英文:
When running the go get command :
sudo go get github.com/go-sql-driver/mysql
I get the following error
> package github.com/go-sql-driver/mysql: cannot download, $GOPATH not
> set. For more details see: go help gopath
However $GOPATH is already set.
Running echo $GOPATH
gives /Users/userxyz/Desktop/Code
Running go env
gives
.....
GOPATH="/Users/userxyz/Desktop/Code"
...
GOROOT="/usr/local/go"
.....
I have already tried setting GOPATH as an environment variable by adding the following lines
export GOPATH="$HOME/Desktop/Code"
export PATH=$PATH:$GOPATH/bin
to the following files, alternatively
~/.profile (/etc/profile)
~/.bashrc
~/.bash_profile
答案1
得分: 5
sudo go get github.com/go-sql-driver/mysql
这个命令在root
用户下运行go get
,但是root
用户没有设置$GOPATH
。
只需要执行以下命令:
go get github.com/go-sql-driver/mysql
通常,在项目文件夹中执行以下命令即可安装所有依赖项:
go get
以下命令将安装测试中提到的依赖项:
go get -t
英文:
sudo go get github.com/go-sql-driver/mysql
This runs go get
under root
user, which does not have $GOPATH
set.
Just do:
go get github.com/go-sql-driver/mysql
Generally, do:
go get
in the project folder, and it will install all dependencies. The following will install dependencies mentioned in tests:
go get -t
答案2
得分: 3
你只需要去掉 sudo
。
你的环境变量是在用户级别定义的。如果你执行 sudo go env
,你会发现 GOPATH
在那里没有设置。
英文:
You just need to drop the sudo
.
Your environment variables are defined at the user level. if you do sudo go env
you'll see that GOPATH
is not set there
答案3
得分: 0
你不需要使用sudo。如果出现“同意Xcode/iOS许可证需要管理员权限”的提示,只需重新打开Xcode并接受Xcode的许可协议即可。
英文:
You don't need sudo. If the "Agreeing to the Xcode/iOS license requires admin privileges" stops you, just re-open Xcode and accept the license agreement of Xcode.
答案4
得分: 0
刚刚意识到写完回答后已经晚了将近4年,但我还是会保留回答,以防其他人可能需要这些答案...
看起来你可能是使用sudo或以root权限创建了该目录。如果你尝试使用普通用户运行$go get
,你会得到一个权限错误,而如果你尝试使用sudo运行,你会得到一个GOPATH未设置的错误。我最近遇到了这个问题,并通过简单地更改目录的所有权来解决它。
export GOPATH=/your/goproject的目录
chown -R ubuntu@staff $GOPATH
cd $GOPATH && go get
这里是一些环境变量,你可以双重检查一下:
export GOPATH=/home/ubuntu/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
请记住,你不需要声明或更改$GOROOT。
最后,你可能想使用[profile.d][1]来导出环境变量:
cat > /etc/profile.d/setgoenv.sh <<EOL
export GOPATH=/home/ubuntu/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOL
希望对你有帮助。
英文:
Just realized after writing the response that it's almost 4 years too late but I'll leave it up just in case there are others who might need the answers...
So it seems like you might have created the directory with sudo or with root privileges. If you try $go get
with a regular user you get a permissions error and when you try it with sudo you get a GOPATH unset error. I've had this problem recently, and I solved it by simply changing the ownership of the directory.
export GOPATH=/directory/of/your/goproject
chown -R ubuntu@staff $GOPATH
cd $GOPATH && go get
Here's just so you have some env variables to double check with:
export GOPATH=/home/ubuntu/work export
PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Keep in mind that there shouldn't be a need for you to declare or change $GOROOT.
Finally, you might want to use [profile.d][1] to export env variables:
cat > /etc/profile.d/setgoenv.sh <<EOL
export GOPATH=/home/ubuntu/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOL
I hope that helped.
答案5
得分: 0
在我的情况下,我导入了一个不存在的错误的GOPATH
和一个错误的GOROOT
到我的PATH
中。
设置export GOROOT=/opt/go
然后export GOPATH=$HOME/work
,确保$HOME/work
存在并且我的用户有访问权限,似乎解决了go get {whattoget}
的问题,然后确保export GOPATH=$HOME/work export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
在我的~/.profile
中。这个帖子中没有任何内容完全适合。
未来,我可能会开始使用Docker来构建我的Go项目,因为这有点麻烦,文档和错误信息都很冗长,但似乎没有以有意义和清晰的方式解释出错的原因(GOROOT
和GOPATH
都已设置,但PATH
、GOROOT
和GOPATH
似乎都有点问题)。
英文:
In my case, I had imported an incorrect GOPATH
that did not exist and an incorrect GOROOT
in my PATH
setting my export GOROOT=/opt/go
then export GOPATH=$HOME/work
, ensuring $HOME/work
existed and had access for my user seems to have solved the problem for go get {whattoget}
, then ensuring export GOPATH=$HOME/work
was in my
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin~/.profile
helped. Nothing in this thread seemed to quite fit.
Going forward I might just start using Docker to build my go projects, as it's a bit of a pain, and the documentation and errors are lengthy but don't seem to explain what is wrong in any meaningful and clear way (GOROOT
and GOPATH
were both setup, but PATH
GOROOT
and GOPATH
all seemed to be a little off).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论