Skip to content

Instantly share code, notes, and snippets.



Created Jan 19, 2018
What would you like to do?
Support for multiple GOPATHs - set GOPATH on change of directory
# with these functions you can have multiple gopath directories residing in /opt, where each gopath folder starts with "go-"
# when cd-ing to one of these directories (or any directory in them, on any level) the GOPATH gets changed automagically
function cd {
# call builtin cd. change to the new directory
builtin cd $@
# call a hook function that can use the new working directory
# to decide what to do
# gopath changer
function set_gopath {
if [[ "$pwd/" =~ ^/opt/go- ]] ; then
gopath_local=$(echo $pwd| cut -d'/' -f 1,2,3)
if [ "$GOPATH" != "$gopath_local" ] ; then
export GOPATH
echo "GOPATH set to $GOPATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.