问题描述
好的,我已经下载了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)
最佳解决办法
您设置的环境变量
$ export GOROOT=$GOPATH
是一个错误。没有任何地方需要或建议这样的设置。实际上,它削弱了Go构建系统所看到的环境。
删除该设置,重新创建您的环境(. bashrc
)或打开一个新终端,它应该工作(如果不存在其他问题)。
另外,如果您没有交叉编译,我建议同时删除以下内容:
export GOARCH=amd64
export GOOS=linux
简而言之,正确导出的GOPATH是唯一的环境变量,在第一次近似中,确实需要它。更多提示here。
编辑:好的,所以我下载了二进制发行版(go1.1.linux-amd64.tar.gz)。引自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.
从这一点可以清楚地看出,您必须没有正确遵循上述说明。修复它,我希望它对你有用。
次佳解决办法
要安装go,首先删除先前安装是很关键的(或者你会得到错误go build fails : runtime/mstkbar.go:151:10: debug.gcstackbarrieroff undefined)
dpkg -l|grep golang # if you see any, run following cmd to remove
sudo apt-get purge golang-*
验证是否仍然安装了go
type go # see if go is installed
安装go时的典型输出
go is hashed (/usr/local/go/bin/go)
如果安装go删除它
sudo rm -rf /usr/local/go # not just /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 # typical value change at will
export PATH=${GOPATH}/bin:${PATH}
获取上述新的env var定义
source ~/.bashrc
验证安装
go version
如果安装正确的典型输出
go version go1.12.1 linux/amd64
—安装完成所以让我们编译一个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
现在粘贴以下来创建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 # run your executable
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)
即使你的安装设置好了也会发生上面的错误…只是发出这个以下载并安装缺少的上游依赖包去(以及他们也可能递归引用的那些)
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
第三种解决办法
TLDR:通过在终端export GOROOT=""
中运行以下命令来取消设置$GOROOT
我在Ubuntu 16.04上安装了Go,一切正常。
然后我错误地按照教程将$GOROOT
设置为$GOPATH
,此时我的构建开始失败说:
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)
我读到的每个解决方案都建议不要设置这个变量,我想解开这个。我找到了以下修复程序来解决它:Bash中的export GOROOT=""
。
将$GOROOT
取消将其设置为空,这是每个人都推荐的,所以要默认返回到原始路径。它为我解决了问题,并且构建开始重新开始工作。