How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

前端 未结 25 2798
死守一世寂寞
死守一世寂寞 2020-11-28 00:45

I am trying to do a go get:

go get github.com/go-sql-driver/mysql

and it fails with the following error:

packa         


        
相关标签:
25条回答
  • 2020-11-28 00:53

    This has been the most annoying thing to deal with. In hopes of saving your time.

    IF go was installed as root user. The root user of your system's bash_profile text file ~/.bash_profile needs to have $GOROOT assigned to the go install directory and $GOPATH needs to be assigned to go /src directory.

      ...$# sudo su
      ...$# vi ~/.bash_profile
    
        ***Story continues in vi editor***
    
        GOROOT=$GOROOT:/usr/local/go
        GOPATH=$GOPATH:/usr/local/go/src
        ...
        [your regular PATH stuff here]
        ...
    

    be sure the path to go binary is in your path on .bash_profile

    PATH=$PATH:$HOME/bin:/usr/local/bin:/usr/local/go/bin

    This PATH can be as long a string it needs to be..to add new items just separate by colon :

    exit vi editor and saving bash profile (:wq stands for write and quit)

      [esc] 
      [shift] + [:] 
      :wq
    

    You have to log out of terminal and log back in for profile to initiate again..or you can just kick start it by using export.

    ...$# export GOPATH=/usr/local/go/src
    

    You can verify go env:

    ...$# go env
    

    Yay!

    GOBIN=""
    GOCHAR="6"
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH="/usr/local/go/src"
    GORACE=""
    GOROOT="/usr/local/go"
    

    Now you can sudo and go will be able to download and create directories inside go/src and you can get down to what you were trying to get done.

    example

    # sudo go get github.com/..
    

    Now you will run into another problem..you might not have git installed..that's another adventure..:)

    0 讨论(0)
  • 2020-11-28 00:55

    (Ubuntu)

    If you don’t set a GOPATH, the default will be used.

    You have to add $GOPATH/bin to your PATH to execute any binary installed in $GOPATH/bin, or you need to type $GOPATH/bin/the-command. Add this to your ~/.bash_profile

    export PATH=$GOPATH/bin:$PATH

    Current GOPATH command:

    go env GOPATH

    Changing the GOPATH command:

    export GOPATH=$HOME/your-desired-path

    0 讨论(0)
  • 2020-11-28 00:55

    My go environment looked similar to yours.

    $go env
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="linux"
    GOOS="linux"
    GOPATH=""
    GORACE=""
    GOROOT="/usr/lib/go-1.6"
    GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="gcc"
    GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
    CXX="g++"
    CGO_ENABLED="1"
    

    I resolved it with setting GOPATH to /usr/lib/go. Try it out.

    export GOPATH=/usr/lib/go
    export PATH=$PATH:$GOPATH/bin
    
    0 讨论(0)
  • 2020-11-28 00:55

    Yet another solution: Export every GO* environment variable from go env

    .bashrc:

    eval $(go env | grep '^GO[A-Z0-9_]*=' | while read setenv; do
      echo "export $setenv; "
    done 2> /dev/null)
    
    [[ -n $GOPATH ]] || export GOPATH="$HOME/go/bin"
    [[ -n $GOROOT ]] || export GOROOT=/usr/bin/go
    export PATH="$PATH:$GOPATH/bin:$GOROOT/bin"
    
    0 讨论(0)
  • 2020-11-28 00:57

    You will need GOPATH later, too. So add it to ~/.bashrc.

    0 讨论(0)
  • 2020-11-28 00:58

    If for example, it is an Ubuntu, after installing the packages:

    $sudo apt install golang -y
    

    Just add the following lines to ~/.bashrc (Of your user)

    export GOROOT=/usr/lib/go
    export GOPATH=$HOME/go
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    0 讨论(0)
提交回复
热议问题