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

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

    You have to update the PATH based on the terminal(bash or zsh) which you use.

    1. Open the shell script file of the terminal i.e ~/.bashrc or ~/.zshrc in an editor
       vi ~/.zshrc
          (or)
       code ~/.zshrc
    
    1. Update the below GOPATH if already found or add the below line.

    export GOPATH=$HOME/go:/$HOME/projects/go

    Here you can add one or more paths separated by a semicolon : from different locations of your GO projects on the system to the GOPATH environment variable i.e /path/1:path/2:path/3 etc.

    In my case, I have added 2 paths, as shown above, one from the root $HOME/go and the other one from the projects directory :/$HOME/projects/go

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

    New Way:

    Check out this answer.

    Old Way:

    Just add the following lines to ~/.bashrc and this will persist. However, you can use other paths you like as GOPATH instead of $HOME/go in my sample.

    export GOPATH=$HOME/go
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    
    0 讨论(0)
  • 2020-11-28 01:05

    This is what got it working for me on Ubuntu 15.10 using the fish shell:

    # ~/.config/fish/config.fish
    
    set -g -x PATH /usr/local/bin $PATH
    set -g -x GOPATH /usr/share/go
    

    Then I had to change the permissions on the go folder (it was set to root)

    sudo chown <name>:<name> -R /usr/share/go
    
    0 讨论(0)
  • 2020-11-28 01:08

    This script will help you switch Gopaths. https://github.com/buffonomics/goswitch

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

    As written in the official instructions:

    The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.

    So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH as /Users/<user>/WebstormProjects.

    In simpler words, set it to wherever you want your Go projects to reside.

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

    Edit your ~/.bash_profile to add the following line:

    $ export GOPATH=$HOME/work
    

    Save and exit your editor. Then, source your ~/.bash_profile

    $ source ~/.bash_profile
    

    Note: Set the GOBIN path to generate a binary file when go install is run

    $ export GOBIN=$HOME/work/bin
    
    0 讨论(0)
提交回复
热议问题