Skip to content

Instantly share code, notes, and snippets.

@clcollins
Created December 9, 2019 14:55
Show Gist options
  • Save clcollins/41b95c134cd7c94a7fad34328f26080c to your computer and use it in GitHub Desktop.
Save clcollins/41b95c134cd7c94a7fad34328f26080c to your computer and use it in GitHub Desktop.
Print all or an index of $GOPATH
# Without an argument, prints the current gopath
# With a integer argument, prints the directory at the index of a multi-directory GOPATH
gopath ()
{
if [ -z "$GOPATH" ]; then
echo "GOPATH not set...";
exit 1;
fi;
local gopath="${GOPATH}";
local max_index="$(awk -F: '{ print NF }' <<< \"${gopath}\")";
local re="^[0-9]+$";
if [[ "${1}" =~ ${re} ]]; then
local index="${1}";
else
printf "${gopath}\n";
return;
fi;
((index++));
if [ "${index}" -gt "${max_index}" ]; then
printf "ERROR: index out of range\n";
fi;
awk -v ind=${index} '{split($0, gopaths, ":"); print gopaths[ind]}' <<< "${gopath}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment