gvm
does not change Go version automatically. This is a little inconvenience when developing multiple projects that use different Go versions. Let's change Go version when moved to the directory of Go project like rbenv
.
Get Go version
Normally, we don't have a file that described Go version(like .ruby-version
, .node-version
).
But since Go 1.12, go.mod
includes Go version 1. This probably the same with the projects' Go version. Let's reuse this.
This can do as following with zsh
.
_change_go_version() {
if [ -f "go.mod" ]; then
local version=(`grep -Po "^go \K([0-9\.]*)$" go.mod`)
gvm use ${version}
fi
}
Change Go version when directory changed
zsh provides some hook functions. One of them, chpwd
executed whenever the current working directory is changed 2. This function can use inside zshrc
. Let's call _change_go_version
inside this function.
# .zshrc
chpwd()
{
_change_go_version
}
As a result, autosomally call gvm use
when moved directory that has the go.mod
file. If you use another version manager, please change gvm use
inside _change_go_version
.
Have a nice day!
Top comments (0)