英文:
Can't get Golang to work in Ubuntu
问题
好的,所以我已经下载了Go 1.1并将其放入$HOME/Documents/go中。
然后,我修改了我的.bashrc
文件如下:
export GOPATH=$HOME/Documents/go
export GOROOT=$GOPATH
export GOARCH=amd64
export GOOS=linux
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
然后我执行了.bashrc
文件,并尝试运行以下命令:
jan@janpc:~$ go version
go version go1.1 linux/amd64
但是我无法编译或安装任何依赖项。
例如,我尝试运行我的小测试程序:
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$ go run scrypt.go
warning: GOPATH set to GOROOT (/home/jan/Documents/go) has no effect
scrypt.go:9:3: cannot find package "github.com/dchest/scrypt" in any of:
/home/jan/Documents/go/src/pkg/github.com/dchest/scrypt (from $GOROOT)
($GOPATH not set)
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$
当我尝试安装依赖项时:
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$ go get "github.com/dchest/scrypt"
warning: GOPATH set to GOROOT (/home/jan/Documents/go) has no effect
package github.com/dchest/scrypt: cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath
在Mac上编译和运行正常。我无法弄清楚我的配置有什么问题,如果我尝试删除$GOROOT
或$GOPATH
,什么都不起作用,我也不知道还应该将它们设置为什么,除了Go的路径。
编辑:
在我的Mac上没有设置$GOROOT。但是如果我在Ubuntu上删除$GOROOT
,当我尝试编译时会出现一堆类似以下的错误。
cannot find package "fmt" in any of:
/usr/local/go/src/pkg/fmt (from $GOROOT)
/home/jan/Documents/go/src/fmt (from $GOPATH)
英文:
Ok, So I've downloaded Go 1.1 and put it into $HOME/Documents/go.
Then, I've modified my .bashrc
to be:
export GOPATH=$HOME/Documents/go
export GOROOT=$GOPATH
export GOARCH=amd64
export GOOS=linux
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
Than I've sourced the .bashrc
, and tried:
jan@janpc:~$ go version
go version go1.1 linux/amd64
But I can't get it to compile or install any dependencies.
Eg. I try to run my little test program:
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$ go run scrypt.go
warning: GOPATH set to GOROOT (/home/jan/Documents/go) has no effect
scrypt.go:9:3: cannot find package "github.com/dchest/scrypt" in any of:
/home/jan/Documents/go/src/pkg/github.com/dchest/scrypt (from $GOROOT)
($GOPATH not set)
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$
And when I try to install dependencies:
jan@janpc:~/Documents/go/src/github.com/jan/scrypt$ go get "github.com/dchest/scrypt"
warning: GOPATH set to GOROOT (/home/jan/Documents/go) has no effect
package github.com/dchest/scrypt: cannot download, $GOPATH must not be set to $GOROOT. For more details see: go help gopath
It compiles and works fine on mac. I can't figure out whats wrong with my config, if I try to remove $GOROOT
or $GOPATH
nothing works, and I don't know what else to set them to, other than the path to Go.
EDIT:
There is no $GOROOT set on my mac. But if I remove $GOROOT
on ubuntu, I get bunch of errors like these when I try to compile.
cannot find package "fmt" in any of:
/usr/local/go/src/pkg/fmt (from $GOROOT)
/home/jan/Documents/go/src/fmt (from $GOPATH)
答案1
得分: 23
你设置的环境变量 $ export GOROOT=$GOPATH
是一个错误。在Go构建系统中,没有任何地方需要或建议这样设置。实际上,这会破坏Go构建系统所看到的环境。
删除该设置,重新创建你的环境(.bashrc
),或者打开一个新的终端,然后它应该可以工作(如果没有其他问题存在的话)。
另外,如果你不是在进行交叉编译,我建议你也删除以下内容:
export GOARCH=amd64
export GOOS=linux
简而言之,正确导出的GOPATH是唯一真正需要的环境变量。这里有一些更多的提示链接。
编辑:好的,所以我已经下载了二进制发行版(go1.1.linux-amd64.tar.gz)。从README中引用:
> 二进制发行版说明
> 如果你刚刚解压了一个二进制Go发行版,你需要将环境变量$GOROOT设置为go目录的完整路径(包含此README文件的目录)。如果你将其解压到/usr/local/go,或者通过运行all.bash从源代码重新构建,你可以省略该变量(参见doc/install.html)。你还应该将Go二进制目录$GOROOT/bin添加到你的shell的路径中。
> 例如,如果你将tar文件解压到$HOME/go,你可以在你的.profile文件中添加以下内容:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
> 更多详细信息请参见doc/install.html。
从这个说明中可以清楚地看出,你肯定没有正确地按照上述说明进行操作。修复这个问题,然后希望它能正常工作。
英文:
Your enviroment variable you've set by
$ export GOROOT=$GOPATH
is a mistake. Nowhere is such setting required nor recommended. Actually, it cripples the environment seen by the Go build system.
Remove that setting, recreate your environment (. bashrc
) or open a new terminal and it should work (if no other problems exists).
Additionally, if you're not cross compiling, I recommend to remove also these:
export GOARCH=amd64
export GOOS=linux
In short, proper exported GOPATH is the only environment variable which is, in the first approximation, really needed. Some more hints here.
EDIT: Okay, so I've downloaded the binary distribution (go1.1.linux-amd64.tar.gz). Quoting from README:
> Binary Distribution Notes
> If you have just untarred a binary Go distribution, you need to set
the environment variable $GOROOT to the full path of the go
directory (the one containing this README). You can omit the
variable if you unpack it into /usr/local/go, or if you rebuild
from sources by running all.bash (see doc/install.html).
You should also add the Go binary directory $GOROOT/bin
to your shell's path.
> For example, if you extracted the tar file into $HOME/go, you might
put the following in your .profile:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
> See doc/install.html for more details.
From this it's clear that you must have not followed properly the above instructions. Fix that and I hope it will work for you then.
答案2
得分: 18
要安装go,首先非常重要删除之前的安装(否则会出现错误 https://stackoverflow.com/q/46009258/147175)
dpkg -l|grep golang # 如果看到任何内容,请运行以下命令进行删除
sudo apt-get purge golang-*
验证go是否仍然安装
type go # 查看是否安装了go
如果go已安装,删除它
sudo rm -rf /usr/local/go # 不仅仅是 /usr/local/go/bin/go
下载最新的tarball https://golang.org/dl/ 并解压安装
new_golang_ver=$(curl https://golang.org/VERSION?m=text 2> /dev/null)
cd /tmp
wget https://dl.google.com/go/${new_golang_ver}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf ${new_golang_ver}.linux-amd64.tar.gz
在你的 ~/.bashrc 中定义这些环境变量
export PATH=/usr/local/go/bin:${PATH}
export GOPATH=${HOME}/gopath # 典型值,根据需要更改
export PATH=${GOPATH}/bin:${PATH}
加载上述新的环境变量定义
source ~/.bashrc
验证安装
go version
如果正确安装,典型输出为
go version go1.12.1 linux/amd64
--- 安装完成,现在编译一个hello world ---
[[ ! -d $GOPATH ]] && mkdir -p ${GOPATH}/{bin,pkg,src} # 如果目录不存在则创建
mkdir -p ${GOPATH}/src/github.com/mygithubname/play
cd ${GOPATH}/src/github.com/mygithubname/play
现在粘贴以下内容以创建go源文件hello_world.go
tee hello_world.go << WOW
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
WOW
编译源代码
go build hello_world.go
./hello_world # 运行可执行文件
> hello, world
要了解如何组织go源代码的结构,请参阅 https://golang.org/doc/code.html
修复缺少的包错误
您还问如何修复缺少的依赖错误...例如
scott@rapacious ~/other_src/gopath/src/github.com/bigeagle/gohop $ go build
hop/cipher.go:27:2: cannot find package "github.com/golang/snappy" in any of:
/usr/local/go/src/github.com/golang/snappy (from $GOROOT)
/home/scott/other_src/gopath/src/github.com/golang/snappy (from $GOPATH)
internal/logging.go:6:2: cannot find package "github.com/op/go-logging" in any of:
/usr/local/go/src/github.com/op/go-logging (from $GOROOT)
/home/scott/other_src/gopath/src/github.com/op/go-logging (from $GOPATH)
即使在正确设置go安装之后,仍会出现上述错误...只需执行以下操作即可下载并安装缺失的上游依赖go包(以及它们可能递归引用的包)
cd ~/Documents/go/src/github.com/jan/scrypt/ # 进入源目录
go get -v -t ./... # -v 详细输出
# -t 也下载任何仅用于测试的包
go build
英文:
To install go it is CRITICAL to first remove prior install (or you will get errors https://stackoverflow.com/q/46009258/147175)
dpkg -l|grep golang # if you see any, run following cmd to remove
sudo apt-get purge golang-*
Verify whether go is still installed
type go # see if go is installed
typical output if go is installed
go is hashed (/usr/local/go/bin/go)
if go is installed remove it
sudo rm -rf /usr/local/go # not just /usr/local/go/bin/go
download the latest tarball https://golang.org/dl/ expand and install
new_golang_ver=$(curl https://golang.org/VERSION?m=text 2> /dev/null)
cd /tmp
wget https://dl.google.com/go/${new_golang_ver}.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf ${new_golang_ver}.linux-amd64.tar.gz
define these environment variables in your ~/.bashrc
export PATH=/usr/local/go/bin:${PATH}
export GOPATH=${HOME}/gopath # typical value change at will
export PATH=${GOPATH}/bin:${PATH}
source your above new env var definitions
source ~/.bashrc
verify install
go version
if installed correctly typical output
go version go1.12.1 linux/amd64
--- install is complete so lets compile a hello world ---
[[ ! -d $GOPATH ]] && mkdir -p ${GOPATH}/{bin,pkg,src} # if no dir then create
mkdir -p ${GOPATH}/src/github.com/mygithubname/play
cd ${GOPATH}/src/github.com/mygithubname/play
now paste following to create the go source file hello_world.go
tee hello_world.go << WOW
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
WOW
compile your source code
go build hello_world.go
./hello_world # run your executable
> hello, world
to understand how to structure your go source code see https://golang.org/doc/code.html
Fix the missing package error
You also asked how to fix the missing dependency errors ... for example
scott@rapacious ~/other_src/gopath/src/github.com/bigeagle/gohop $ go build
hop/cipher.go:27:2: cannot find package "github.com/golang/snappy" in any of:
/usr/local/go/src/github.com/golang/snappy (from $GOROOT)
/home/scott/other_src/gopath/src/github.com/golang/snappy (from $GOPATH)
internal/logging.go:6:2: cannot find package "github.com/op/go-logging" in any of:
/usr/local/go/src/github.com/op/go-logging (from $GOROOT)
/home/scott/other_src/gopath/src/github.com/op/go-logging (from $GOPATH)
above error will happen even after your go install is setup OK ... just issue this to download and install the missing upstream dependency go packages (and the ones they might also reference recursively)
cd ~/Documents/go/src/github.com/jan/scrypt/ # cd into the source dir
go get -v -t ./... # -v verbose
# -t also download any test only packages
go build
答案3
得分: 9
TLDR: 在终端中运行以下命令export GOROOT=""
来取消设置$GOROOT
。
我在Ubuntu 16.04上安装了Go,一切都正常工作。
然后我错误地将$GOROOT
设置为$GOPATH
,按照教程的指示,此时我的构建开始失败,显示以下错误信息:
警告:设置为GOROOT的GOPATH无效
无法在以下任何位置找到包“fmt”:
...(来自$GOROOT)(未设置$GOPATH)
无法在以下任何位置找到包“io/ioutil”:
导入运行时:无法在以下任何位置找到包“runtime”:
/home/mhsn/go/src/runtime(来自$GOROOT)
(未设置$GOPATH)
我阅读的每个解决方案都建议不要设置这个变量,我想取消设置。我找到了以下修复方法来取消设置:在bash中运行export GOROOT=""
。
将$GOROOT
取消设置为空,这是每个人推荐的做法,这样go会恢复到原始路径。这解决了我的问题,go build又开始工作了。
英文:
TLDR: Unset $GOROOT
by running following command in your terminal export GOROOT=""
I had Go installed on Ubuntu 16.04 and everything was working fine.
Then by mistake I set the $GOROOT
to $GOPATH
following a tutorial, at which point my build started to fail saying:
warning: GOPATH set to GOROOT has no effect
cannot find package "fmt" in any of:
... (from $GOROOT) ($GOPATH not set)
cannot find package "io/ioutil" in any of:
imports runtime: cannot find package "runtime" in any of:
/home/mhsn/go/src/runtime (from $GOROOT)
($GOPATH not set)
Every solution I read was suggesting to not set this variable, and I wanted to unset this. I found the following fix to get it unset: export GOROOT=""
in bash.
Having unset the $GOROOT
back to empty which was recommended by everyone so go defaulted back to original path. And it fixed the issue for me and go build started to work again.
答案4
得分: 2
你不应该为正常安装设置$GOROOT。
只需键入export GOROOT=""
,它应该解决你的问题。
英文:
You should not set $GOROOT for normal installations.
Just type export GOROOT=""
and it should fix your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论