This simple script makes the go tools available to your awesome Snap CI continuous integration build.

It does the following:

  • Set all required environment variables
  • If go is not cached
    • Downloads go-(1.4)
    • Puts it in the snap cache
    • Installs vet

You can simply call it as a pinned build step via:

source setup_go.sh <GO_PATH> <GO_VERSION> <GO_OS> <GO_ARCH>

For the default argument values, see the script. The script assumes that your repository is the root of a valid GOPATH.

#!/bin/bash
# Pin this script in your build steps.

# Default values for go-1.4 linux-amd64
#
# see: http://golang.org/dl/
#
GOPATH=${1:-`pwd`}
GO_VERSION=${2:-"1.4"}
GO_OS=${3:-"linux"}
GO_ARCH=${4:-"amd64"}

echo "======================================"
echo "Setting up go-$GO_VERSION environment."
echo "======================================"
echo "GOPATH = $GOPATH"
export GOROOT="$SNAP_CACHE_DIR/go-$GO_VERSION"
echo "GOROOT = $GOROOT"
export PATH="$PATH:$SNAP_CACHE_DIR/go-$GO_VERSION/bin"
echo "PATH = $PATH"
echo "======================================"
echo ""

# If we have not downloaded go yet, do it and put it in the cache
if [ ! -d $GOROOT ] ; then
mkdir $GOROOT

GO_ARTIFACT="go$GO_VERSION.$GO_OS-$GO_ARCH.tar.gz"
LOCAL_PATH="$SNAP_CACHE_DIR/$GO_ARTIFACT"
echo "Downloading $GO_ARTIFACT"

wget -O $LOCAL_PATH https://storage.googleapis.com/golang/$GO_ARTIFACT
tar -xzf $LOCAL_PATH --strip 1 -C $GOROOT

# Install vet
go get golang.org/x/tools/cmd/vet
fi