Install Go Versioned

go Linux

This script extends the previous one by installing GO into a version directory and having the environment variables point to a current link

#! /usr/bin/env bash

VERSION=${VERSION:-1.10.2}

golangurl=https://dl.google.com/go/go${VERSION}.linux-amd64.tar.gz
downloaddir="${HOME}/tmp/go-download"
basedir="${HOME}/.local/go"
installdir="${HOME}/.local/go/_${VERSION}"
workspacedir="${HOME}/go"

echo "This script installs Golang into your home folder"
read -p "Press enter to continue"

if [ -d "${basedir}/${VERSION}" ]; then
  echo "seems to be installed"
  exit 1
fi

echo "Creating download directory ${downloaddir}"
mkdir -p ${downloaddir}

echo "Downloading Golang"
wget "${golangurl}" -O "${downloaddir}/go1.10.linux-amd64.tar.gz"

echo "Extracting Golang distribution"
mkdir -p "${installdir}"
tar -C "${installdir}" -xzf "${downloaddir}/go1.10.linux-amd64.tar.gz"
rm "${downloaddir}/go1.10.linux-amd64.tar.gz"
mv "${installdir}/go" "${basedir}/${VERSION}"
rm "${basedir}/current"
ln -s "${basedir}/${VERSION}" "${basedir}/current"
rm -rf "${installdir}"

echo "Creating go workspace"
mkdir -p "${workspacedir}/bin"
mkdir -p "${workspacedir}/src"
mkdir -p "${workspacedir}/pkg"

echo "export GOROOT=${basedir}/current" > "${HOME}/.go-profile"
echo "export GOPATH=${HOME}/go" >> "${HOME}/.go-profile"
echo "export PATH=${basedir}/${current}/go/bin:${GOROOT}/bin:${PATH}" >> "${HOME}/.go-profile"

echo "sourcing ~/.go-profile"
source "${HOME}/.go-profile"

echo "Testing"
go version

read -p "Going to add source to ${HOME}/.profile, if unwanted press Ctrl-C now"

echo >> "${HOME}/.profile"
echo >> "${HOME}/.profile"
echo "# Set go specific environment" >> "${HOME}/.profile"
echo ". ~/.go-profile" >> "${HOME}/.profile"

Update: Fixing path Update 2: removing installdir