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

前端 未结 25 3027
死守一世寂寞
死守一世寂寞 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..:)

提交回复
热议问题