moovweb / gvm

Go Version Manager
http://github.com/moovweb/gvm
MIT License
9.91k stars 522 forks source link

Enhancement: Have a file that can configure which version of go is used #383

Open kwsorensen opened 3 years ago

kwsorensen commented 3 years ago

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.

nilesh-akhade commented 2 years ago

We can also use go directive in the go.mod file

derekjobst commented 2 years ago

Perhaps the .go-version standard could also be supported?

derekjobst commented 2 years ago

Doesn't .gvm_local work for this?

ryan-dyer-sp commented 1 year ago

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.

ivankatliarchuk commented 10 months ago

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