Skip to content

Instantly share code, notes, and snippets.

@milas
Last active September 21, 2022 15:06
Show Gist options
  • Save milas/cdd84f9edba318b5f2719b78f436a4ba to your computer and use it in GitHub Desktop.
Save milas/cdd84f9edba318b5f2719b78f436a4ba to your computer and use it in GitHub Desktop.
Extract Go version from go.mod
# These have been tested against:
# macOS 12.6 / built-in
# Alpine 3.16.2 / BusyBox v1.35
# Debian 11.5 / GNU sed v4.7
# short, readable, should be fine for most purposes!
sed -n -e 's/^go \(.*\)$/\1/p' go.mod
# strict based on https://go.dev/ref/mod#go-mod-file-go
# > The version must be a valid Go release version: a positive
# > integer followed by a dot and a non-negative integer
# > (for example, 1.9, 1.14).
sed -n -e 's/^go \([[:digit:]][[:digit:]]*\.[[:digit:]][[:digit:]]*\)$/\1/p' go.mod
# don't forget to escape the $ in Makefile!
GO_VERSION=$(shell sed -n -e 's/^go \(.*\)$$/\1/p' go.mod)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment