Open kwsorensen opened 3 years ago
We can also use go
directive in the go.mod
file
Perhaps the .go-version
standard could also be supported?
Doesn't .gvm_local
work for this?
Not sure if this is what the author desired, but I'd like something similar to pyenv's behavior where just being in the directory itself modifies the go executable/version.
With ZSH this code I'm using to achieve exactly ^ that
# Automatically switch and load golang versions when a directory has an `.gvmrc` or `go.mod` files
load-go-version() {
if exists gvm; then
if [ -f .gvmrc ] || [ -f go.mod ]; then
local GO_VERSION=$(go version | { read _ _ v _; echo ${v#go}; })
if [ -f .gvmrc ]; then
local GO_GVMRC_VERSION=$(cat .gvmrc)
if ! go version | grep "$GO_GVMRC_VERSION" >/dev/null 2>&1; then
gvm use $GO_GVMRC_VERSION >/dev/null 2>&1
fi
if [ $? -eq 1 ]
then
gvm install $GO_GVMRC_VERSION
gvm use $GO_GVMRC_VERSION >/dev/null 2>&1
fi
fi
if [ -f go.mod ]; then
local GO_LOCAL_VERSION=$(go list -f {{.GoVersion}} -m)
if [[ "$GO_VERSION" == "$GO_LOCAL_VERSION" ]]; then
# echo "version match"
else
gvm use ${GO_LOCAL_VERSION} 2>&1
fi
if [ $? -eq 1 ]
then
gvm install go${GO_LOCAL_VERSION}
gvm use ${GO_LOCAL_VERSION} 2>&1
fi
fi
fi
fi
}
add-zsh-hook chpwd load-go-version
load-go-version
Scenario:
Using multiple versions of golang in different repositories.
Repo 1: go1.14.15 Repo 2: go1.16.5
Solution:
Have a .gvm at the root of each repository that determines which version of go should be used for each Repository.