gvm icon indicating copy to clipboard operation
gvm copied to clipboard

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

Open kwsorensen opened this issue 4 years ago • 5 comments

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.

kwsorensen avatar Jul 14 '21 16:07 kwsorensen

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

nilesh-akhade avatar Aug 25 '21 11:08 nilesh-akhade

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

derekjobst avatar Nov 30 '21 21:11 derekjobst

Doesn't .gvm_local work for this?

derekjobst avatar Nov 30 '21 21:11 derekjobst

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.

ryan-dyer-sp avatar Nov 09 '22 20:11 ryan-dyer-sp

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

ivankatliarchuk avatar Sep 02 '23 12:09 ivankatliarchuk