On Wed, Aug 09, 2017 at 03:11:48PM +0200, Michael Banzon wrote:

> Is there a way to have a (bash) script check if the version of the Go
> compiler installed is a specific minimum version?

In the light of [1], I think you could combine checking of the presense
of the `go` tool itself with build tags.
Say, something like this (untested):

---------------->8----------------
  set -eu
  
  rc=0
  go version >/dev/null || rc=$?
  if [ $rc -ne 0 ]; then
    echo "The 'go' tool is not available" >&2
    exit 2
  fi
  
  d=`mktemp -d`
  f=`mktemp`
  trap "rm -rf '$d' '$f'" EXIT INT TERM QUIT
  cd "$d"
  
  cat >false.go <<'EOF'
  // +build !go1.7
  package main
  
  import "os"
  
  func main() {
    os.Exit(1)
  }
  EOF
  
  cat >true.go <<'EOF'
  // +build go1.7
  package main
  
  import "os"
  
  func main() {
    os.Exit(0)
  }
  EOF
  
  go build -o "$f" "$d/*.go"
  
  rc=0
  "$f" || rc=1
  
  if [ $rc -ne 0 ]; then
    echo "Insufficient Go version" >&2
    exit 2
  fi
  
  exit 0
---------------->8----------------

One possible caveat is that `mktemp` by default creates its filesystem
entries under $TMPDIR or /tmp, and that directory might be mounted with
"noexec" on certain systems.  So if your setup script (or whatever it
is) has a luxury of using its own scratch space, you should probably do
that (like creating a temp. directory using `mktemp -f ~/.cache/XXXXXX`
or something like this).

1. https://github.com/golang/go/issues/21207

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to