Skip to content

Instantly share code, notes, and snippets.

@lovromazgon lovromazgon/.bashrc
Created Jan 19, 2018

Embed
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
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
You can’t perform that action at this time.