Skip to content

Instantly share code, notes, and snippets.

@lovromazgon
Created January 19, 2018 12:33
Show Gist options
  • Save lovromazgon/ac7c20ce66911985f892d86a41ec2dbf to your computer and use it in GitHub Desktop.
Save lovromazgon/ac7c20ce66911985f892d86a41ec2dbf to your computer and use it in GitHub Desktop.
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
set_gopath
}
# gopath changer
function set_gopath {
pwd=$(pwd)
if [[ "$pwd/" =~ ^/opt/go- ]] ; then
gopath_local=$(echo $pwd| cut -d'/' -f 1,2,3)
if [ "$GOPATH" != "$gopath_local" ] ; then
GOPATH=$gopath_local
export GOPATH
echo "GOPATH set to $GOPATH"
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment