问题描述

好的,我已经下载了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
$GOROOT$GOPATH
$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。

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

第三种解决办法

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)
export GOROOT=""
$GOROOT

参考资料