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

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

    On my Fedora 20 machine I added the following lines to my ~/.bashrc:

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

    You can use the "export" solution just like what other guys have suggested. I'd like to provide you with another solution for permanent convenience: you can use any path as GOPATH when running Go commands.

    Firstly, you need to download a small tool named gost : https://github.com/byte16/gost/releases . If you use ubuntu, you can download the linux version(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz).

    Then you need to run the commands below to unpack it :

    $ cd /path/to/your/download/directory 
    $ tar -xvf gost_linux_amd64.tar.gz
    

    You would get an executable gost. You can move it to /usr/local/bin for convenient use:

    $ sudo mv gost /usr/local/bin
    

    Run the command below to add the path you want to use as GOPATH into the pathspace gost maintains. It is required to give the path a name which you would use later.

    $ gost add foo /home/foobar/bar     # 'foo' is the name and '/home/foobar/bar' is the path
    

    Run any Go command you want in the format:

    gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]
    

    For example, you want to run go get github.com/go-sql-driver/mysql with /home/foobar/bar as the GOPATH, just do it as below:

    $ gost get -p foo -- github.com/go-sql-driver/mysql  # 'foo' is the name you give to the path above.
    

    It would help you to set the GOPATH and run the command. But remember that you have added the path into gost's pathspace. If you are under any level of subdirectories of /home/foobar/bar, you can even just run the command below which would do the same thing for short :

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

    gost is a Simple Tool of Go which can help you to manage GOPATHs and run Go commands. For more details about how to use it to run other Go commands, you can just run gost help goCmdName. For example you want to know more about install, just type words below in:

    $ gost help install
    

    You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md

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

    At the end of the ~.profile file add::

    export GOPATH="$HOME/go"
    export PATH="$PATH:/usr/local/go/bin"
    export PATH="$PATH:$GOPATH/bin"
    
    0 讨论(0)
  • 2020-11-28 01:02

    GOPATH should be set to a newly created empty directory. This is the go "workspace", where it downloads packages, et cetera. I use ~/.go.

    For example:

    mkdir ~/.go
    echo "GOPATH=$HOME/.go" >> ~/.bashrc
    echo "export GOPATH" >> ~/.bashrc
    echo "PATH=\$PATH:\$GOPATH/bin # Add GOPATH/bin to PATH for scripting" >> ~/.bashrc
    source ~/.bashrc
    

    source: http://www.larry-price.com/blog/2013/12/15/setting-up-a-go-environment-in-ubuntu-12-dot-04/

    0 讨论(0)
  • 2020-11-28 01:02

    With Go 1.8 (Q2 2017), you won't have to edit any file if you accept the default GOPATH value (set for you)

    $HOME/go
    

    You can see the comments on issue 17262 and the associated twitter vote:

    By choosing a default GOPATH, we can make our documentation easier because we can say things like

    $ go get github.com/foo/bar
    

    will check out the github.com/foo/bar repo into $HOME/go/src/github.com/foo/bar.

    We don't need to distract newcomers with having to set an env var, all we need to do is put a little parenthetical note at the bottom of the page

    $HOME/go is the default path to your go workspace.
    If you want to change this path, set the GOPATH variable to something of your choosing.

    0 讨论(0)
  • 2020-11-28 01:02

    GOPATH is an environment variable to your work-space location. GOROOT is an environment variable to your installation directory. Although GOROOT and GOPATH is automatically set (if there would not be a bug) during the installation time, to specify it manually you can follow below process. Moreover, for more information you can refer to GO's wiki page.

    It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).

    1. This is a solution mac, which is tested on macOS Sierra, ver. 10.12, and also in Gogland-EAP, which has been introduced as an IDE for Go programming by JetBrains.

    On your Terminal type

    vim ~/.profile
    

    in opened document on the Terminal press i and add the following path

    GOPATH=/path/to/a/directory/inside/home/directory
    GOROOT=/path/to/you/go/library
    PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin
    

    press ESC and type :x. Lastly, you should restart (close and open) your terminal or Logout and Login again.

    1. For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.

    CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.

    0 讨论(0)
提交回复
热议问题