How to Add Multiple Directories in GOPATH with VSCode?

Since go versions keep moving up, maintaining GOPATH inside a Go installation path is a pain.

export GOPATH=/home/a-user/go/1.14.9
ls /home/a-user/go/1.14.0/src 
bitbucket.org github.com    go.uber.org   golang.org    gopkg.in

Adding multiple directories in GOPATH

  1. Create a directory e.g. for work
    mkdir $HOME/mywork
    
  2. GOPATH assumes a structure in the directory, so create three directories inside.
    ls $HOME/mywork
    bin pkg src
    
  3. You will do all your personal development work inside $HOME/mywork/src.
    ls $HOME/mywork/src
    project-01
    
  4. Append .bashrc or .zshrc (append is a must, it won’t work otherwise because GOPATH is preset by goenv)
    # keep it at the end of the file
    export GOENV_ROOT="$HOME/.goenv"
    export PATH="$GOENV_ROOT/bin:$PATH"
    eval "$(goenv init -)"
    
    export PATH="$GOROOT/bin:$PATH"
    export PATH="$PATH:$GOPATH/bin"
    export PATH="$PATH:$HOME/bin"
    
    # add personal directory
    export GOPATH="$GOPATH:$HOME/mywork/"
    
  5. Open the directory $HOME/mywork/src/project-01 in VSCode workspace.
  6. If you have the VSCode extension for Go installed, restart VSCode.

If go to implementation doesn’t work, ensure that go is installed properly with src directory.

ls $HOME/go/1.14.0/ 
bin pkg src

That’s all folks!

References

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: