25

好的,所以我已经下载了 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)
4

4 回答 4

23

您设置的环境变量

$ export GOROOT=$GOPATH

是一个错误。任何地方都不需要也不推荐这样的设置。实际上,它削弱了 Go 构建系统所看到的环境。

删除该设置,重新创建您的环境 ( . bashrc) 或打开一个新终端,它应该可以工作(如果不存在其他问题)。

此外,如果您不进行交叉编译,我建议您也删除这些:

export GOARCH=amd64
export GOOS=linux

简而言之,正确导出的 GOPATH 是唯一真正需要的环境变量。这里还有一些提示。

编辑:好的,所以我已经下载了二进制发行版(go1.1.linux-amd64.tar.gz)。引用自述文件:


二进制发行说明

如果你刚刚解压了一个二进制 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。


由此可见,您一定没有正确遵循上述说明。解决这个问题,我希望它对你有用。

于 2013-06-07T06:38:41.577 回答
18

要安装 go ,首先删除之前的安装是至关重要的(否则你会得到错误go build failed : 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

下载最新的压缩包 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}

获取上述新环境变量定义

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 

你好世界


要了解如何构建您的 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 install 设置正常,上述错误也会发生......只需发出此命令即可下载并安装缺少的上游依赖项 go 包(以及它们也可能递归引用的包)

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
于 2016-12-25T19:04:59.070 回答
9

TLDR: 通过在终端中运行以下命令取消设置$GOROOTexport 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=""在 bash.

将每个人都推荐的背面取消设置$GOROOT为空,因此默认返回原始路径。它为我解决了这个问题,并且 go build 又开始工作了。

于 2016-12-24T17:00:33.857 回答
2

您不应该为正常安装设置 $GOROOT。

只需键入export GOROOT="",它应该可以解决您的问题。

于 2017-06-19T13:27:35.110 回答