Last active
May 26, 2022 03:13
-
-
Save wbailey/88983f497089de5f09b8 to your computer and use it in GitHub Desktop.
Automatically add the current directory to the GOPATH when changing into it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GOPATH helper | |
goPathFile="$HOME/.gopathsrc" | |
if [ -f "$goPathFile" ]; then | |
source "$goPathFile" | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This solution will constantly increase the number of paths within GOPATH
. You need to modify it by keeping a "constant" part of the GOPATH in a separate environment and exporting GOPATH in setGoPath()
by concatenating the "constant" path with $PWD
.
@minherz There is no downside to having repeated elements in the path while foregoing the complexity of maintaining the unique elements you are suggesting.
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
GOPATH Helper
Setup your shell to automatically add a directory to the GOPATH environment variable when you change directories
I got tired of setting the GOPATH in an unintelligent way for my projects so I created these helpers.