Skip to content

Instantly share code, notes, and snippets.

@sathishvj
Created December 29, 2016 09:52
Show Gist options
  • Save sathishvj/d9c8daef9382e55d8cdfceed9c5a2c90 to your computer and use it in GitHub Desktop.
Save sathishvj/d9c8daef9382e55d8cdfceed9c5a2c90 to your computer and use it in GitHub Desktop.
bash function to set gopath automatically if you are already under a src dir
# often times I find myself doing a `go build` and then realizing that this project has a different GOPATH
# I call the below bash function I am in the go code dir.
# It will get the pwd and set the GOPATH automatically to the folder in which there is a "/src/"
# depending on whether you are on linux or mac, you can simplify the sed to use the extended options -r or -e respectively, but I've kept it universal for now
function setgopath() {
pwd=`pwd`
newGoPath=`echo $pwd | sed 's/\(.*\)\/src\/.*/\1/'`
if [ -z $newGoPath ]; then
echo "GOPATH not changed"
else
export GOPATH=$newGoPath
fi
echo "GOPATH="$GOPATH
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment