Install Personal Golang

go Linux

Problem

I wanted to set up a Golang environment on a Linux machine and do it repeatedly

Solution

I created a bash script that downloads Golang and creates a working environment which you can find below.

You could also go with gvm which I have not used but looks promising.

Using an environment variable for VERSION you can set the Golang version to install.

#! /usr/bin/env bash

VERSION=${VERSION:-1.10.1}

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

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

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"

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

echo "export GOROOT=${HOME}/go-distribution/go" > "${HOME}/.go-profile"
echo "export GOPATH=${HOME}/go" >> "${HOME}/.go-profile"
echo "export PATH=${HOME}/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"