Skip to content

Instantly share code, notes, and snippets.

@blockloop
Last active October 13, 2021 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blockloop/f9c415764732930503990a7fc6f426ed to your computer and use it in GitHub Desktop.
Save blockloop/f9c415764732930503990a7fc6f426ed to your computer and use it in GitHub Desktop.
Automatically use the correct go version for your project with direnv + goenv
#!/bin/bash
#
# see: https://direnv.net/
# see: https://github.com/syndbg/goenv
#
# Place this in your go project root as .envrc
GOMOD_VERSION=$(grep 'go 1\.' go.mod | cut -d' ' -f2)
if [[ $GOMOD_VERSION =~ ^1\.[0-9]+$ ]]; then
GOMOD_VERSION="${GOMOD_VERSION}.0"
fi
if ! goenv versions | grep -q $GOENV_VERSION 2> /dev/null; then
goenv install $GOENV_VERSION
fi
export GOENV_VERSION=$GOMOD_VERSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment