无法让Golang在Ubuntu中工作

if *_*one 21 ubuntu go

好的,我已经下载了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
Run Code Online (Sandbox Code Playgroud)

比我来源了.bashrc,并试过:

jan@janpc:~$ go version
go version go1.1 linux/amd64
Run Code Online (Sandbox Code Playgroud)

但我无法编译或安装任何依赖项.例如.我尝试运行我的小测试程序:

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$ 
Run Code Online (Sandbox Code Playgroud)

当我尝试安装依赖项时:

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
Run Code Online (Sandbox Code Playgroud)

它编译并在mac上正常工作.我无法弄清楚我的配置有什么问题,如果我尝试删除$GOROOT$GOPATH没有任何作用,我不知道还有什么设置它们,除了Go的路径.

编辑:我的mac上没有设置$ GOROOT.但是如果我$GOROOT在ubuntu上删除,当我尝试编译时,我会收到类似这样的错误.

cannot find package "fmt" in any of:
    /usr/local/go/src/pkg/fmt (from $GOROOT)
    /home/jan/Documents/go/src/fmt (from $GOPATH)
Run Code Online (Sandbox Code Playgroud)

zzz*_*zzz 21

您设置的环境变量

$ export GOROOT=$GOPATH
Run Code Online (Sandbox Code Playgroud)

是一个错误.无论如何都不需要这样的设置.实际上,它削弱了Go构建系统所看到的环境.

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

另外,如果您没有交叉编译,我建议同时删除以下内容:

export GOARCH=amd64
export GOOS=linux
Run Code Online (Sandbox Code Playgroud)

简而言之,正确导出的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
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅doc/install.html.


从这一点可以清楚地看出,您必须没有正确遵循上述说明.修复它,我希望它对你有用.

  • `echo $ GOROOT`是空白的,但是`go env`报告`GOROOT ="/ usr/local/go"`.但是没有/ usr/local/go! (3认同)

Sco*_*and 16

要安装go,首先删除之前安装是很关键的(或者你会得到错误,构建失败: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-*
Run Code Online (Sandbox Code Playgroud)

验证是否仍然安装了go

type go    # see if go is installed
Run Code Online (Sandbox Code Playgroud)

安装go时的典型输出

go is hashed (/usr/local/go/bin/go)
Run Code Online (Sandbox Code Playgroud)

如果安装go删除它

sudo rm -rf /usr/local/go     #  not just /usr/local/go/bin/go
Run Code Online (Sandbox Code Playgroud)

下载最新的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
Run Code Online (Sandbox Code Playgroud)

在〜/ .bashrc中定义这些环境变量

export PATH=/usr/local/go/bin:${PATH}
export GOPATH=${HOME}/gopath  # typical value change at will
export PATH=${GOPATH}/bin:${PATH}
Run Code Online (Sandbox Code Playgroud)

获取上述新的env var定义

source ~/.bashrc  
Run Code Online (Sandbox Code Playgroud)

验证安装

go version
Run Code Online (Sandbox Code Playgroud)

如果安装正确的典型输出

go version go1.12.1 linux/amd64
Run Code Online (Sandbox Code Playgroud)

--- 安装完成所以让我们编译一个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
Run Code Online (Sandbox Code Playgroud)

现在粘贴以下来创建go源文件hello_world.go

tee hello_world.go  << WOW

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n")
}

WOW
Run Code Online (Sandbox Code Playgroud)

编译你的源代码

go build hello_world.go 

./hello_world     #  run your executable 
Run Code Online (Sandbox Code Playgroud)

你好,世界


要了解如何构建源代码,请参阅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)
Run Code Online (Sandbox Code Playgroud)

即使你的安装设置好了也会发生上面的错误...只是发出这个以下载并安装缺少的上游依赖包去(以及他们也可能递归引用的那些)

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
Run Code Online (Sandbox Code Playgroud)


Moh*_*sin 7

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)
Run Code Online (Sandbox Code Playgroud)

我阅读的每个解决方案都建议不要设置此变量,而我想取消设置。我发现以下修复程序使其未设置:export GOROOT=""在bash中。

$GOROOT每个人都建议将后退设置为空,因此默认返回到原始路径。它为我解决了这个问题,开始构建再次开始工作。