Skip to content

Instantly share code, notes, and snippets.

@wbailey
Last active May 26, 2022 03:13
Show Gist options
  • Save wbailey/88983f497089de5f09b8 to your computer and use it in GitHub Desktop.
Save wbailey/88983f497089de5f09b8 to your computer and use it in GitHub Desktop.
Automatically add the current directory to the GOPATH when changing into it
# GOPATH helper
goPathFile="$HOME/.gopathsrc"
if [ -f "$goPathFile" ]; then
source "$goPathFile"
fi
CHECK_FILENAME=.gopath
setGoPath() {
if [ -f "$PWD/$CHECK_FILENAME" ]; then
if ! [[ "$GOPATH" =~ (^|:)"$PWD"(:|$) ]]; then
echo "Adding $PWD to GOPATH"
export GOPATH=$GOPATH:$PWD
echo $GOPATH
fi
fi
}
if [ "$PROMPT_COMMAND" != "*setGoPath*" ]; then
export PREV_PROMPT_COMMAND=$PROMPT_COMMAND
export PROMPT_COMMAND="$PROMPT_COMMAND; setGoPath"
fi
@minherz
Copy link

minherz commented Dec 20, 2020

As you wish. I would not dispute about the "foregoing maintenance complexity" because it depends on the observer. Mind though that the longer the path the more time is required to process things and more chances for ambiguity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment