doumao9363
doumao9363
2013-06-07 06:33

无法让Golang在Ubuntu中工作

已采纳

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)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doz22551 doz22551 8年前

    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.

    点赞 评论 复制链接分享
  • douyun8901 douyun8901 5年前

    To install go it is CRITICAL to first remove prior install (or you will get errors 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-*
    

    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
    ")
    }
    
    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
    
    点赞 评论 复制链接分享
  • duanmei1350 duanmei1350 5年前

    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.

    点赞 评论 复制链接分享
  • doujiao2000 doujiao2000 4年前

    You should not set $GOROOT for normal installations.

    Just type export GOROOT="" and it should fix your problem.

    点赞 评论 复制链接分享

相关推荐