I have been using GoLang for a while and wrote this simple bash script which can help with installing GoLang on your Mac or Linux machine and also, setting up the environmental variables for you.

Note: This script is tested on Mac OS X and Ubuntu.

This script will try to install a particular version, if povided to it via the -v flag or else, it will try to install Go 1.7.3 (which is the stable version as of writing this script). The example in the Installing Demo section shows you how to install golang 1.7.5 on ubuntu.


#!/usr/bin/env bash
set -e 

# Author: Ravi Teja Pothana (@RaviTezu)
# Date: Nov 15, 2016

# The MIT License (MIT)

# Copyright (c) 2016 RaviTezu

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

GO_VERSION="1.7.3"
OS=`uname -s`
HOME_DIR=$HOME
GO_HOME=$HOME_DIR/go
GO_ROOT=/usr/local/go
ARCH=`uname -m`

function usage {
    printf "$0 -v <version> \n"
    printf "Example: $0 -v 1.7.3 \n"
    exit 1
}

while getopts ":v:" opt; do
  case $opt in
    v) GO_VERSION="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    echo 
    usage
    ;;
  esac
done 

# Function to install golang and setup the env.
function install {
    echo
    echo "...... [ Scanning ]"
    # Check if there's any older version of GO installed on the machine. 
    if [ -d /usr/local/go ]; then 
        echo "...... [ Found an older version of GO ]"
        printf "Would you like to remove it? [y/N]: "
        read ans
        case "$ans" in 
            "y"|"yes"|"Y"|"Yes"|"YES") sudo rm -rf /usr/loca/go;;
            *) echo "...... [ Exiting ]"; exit 0;;
        esac
    fi
    # If the operating system is 64-bit Linux
    if [ "$OS" == "Linux" ] && [ "$ARCH" == "x86_64" ]; then
        PACKAGE=go$GO_VERSION.linux-amd64.tar.gz
        pushd /tmp > /dev/null
            echo
            echo "...... [ Downloading ]"
            wget https://storage.googleapis.com/golang/$PACKAGE
            if [ $? -ne 0 ]; then 
                echo "Failed to Download the package! Exiting."
                exit 1
            fi
            echo "...... [ Installing ]"
            sudo tar -C /usr/local -xzf $PACKAGE
            rm -rf $PACKAGE
        popd > /dev/null
        echo "...... [ Installed ]"
        setup
        echo "...... [ Open a new terminal tab to start using GO ]"
        exit 0
    fi

    # If the operating system is 64-bit MacOS
    if [ "$OS" == "Darwin" ] && [ "$ARCH" == "x86_64" ]; then 
        PACKAGE=go$GO_VERSION.darwin-amd64.pkg
        pushd /tmp  > /dev/null
            echo
            echo "...... [ Downloading ]"
            wget https://storage.googleapis.com/golang/$PACKAGE
            if [ $? -ne 0 ]; then 
                echo "Failed to Download the package! WTF!"
                exit 1
            fi
            echo "...... [ Installing ]"
            sudo /usr/sbin/installer -pkg $PACKAGE -target /
            rm -rf $PACKAGE
        popd > /dev/null
        echo "...... [ Installed ]"
        setup
        echo "...... [ Open a new terminal tab to start using GO ]"
        exit 0
    fi

    # Couldn't determine the machine arch or the operating system. So error out mate!'
    errorout
}

function setup {
    # Create GOHOME and the required directories
    if [ ! -d $GO_HOME ]; then
        mkdir $GO_HOME
        mkdir -p $GO_HOME/{src,pkg,bin}
    else
        mkdir -p $GO_HOME/{src,pkg,bin}
    fi

    if [ "$OS" == "Linux" ] && [ "$ARCH" == "x86_64" ]; then
        grep -q -F 'export GOPATH=$HOME/go' $HOME/.bashrc || echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
        grep -q -F 'export GOROOT=/usr/local/go' $HOME/.bashrc || echo 'export GOROOT=/usr/local/go' >> $HOME/.bashrc
        grep -q -F 'export PATH=$PATH:$GOROOT/bin' $HOME/.bashrc || echo 'export PATH=$PATH:$GOROOT/bin' >> $HOME/.bashrc
        grep -q -F 'export PATH=$PATH:$GOPATH/bin' $HOME/.bashrc || echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bashrc  
    fi

    if [ "$OS" == "Darwin" ] && [ "$ARCH" == "x86_64" ]; then
        grep -q -F 'export GOPATH=$HOME/go' $HOME/.bash_profile || echo 'export GOPATH=$HOME/go' >> $HOME/.bash_profile
        grep -q -F 'export GOROOT=/usr/local/go' $HOME/.bash_profile || echo 'export GOROOT=/usr/local/go' >> $HOME/.bash_profile
        grep -q -F 'export PATH=$PATH:$GOROOT/bin' $HOME/.bash_profile || echo 'export PATH=$PATH:$GOROOT/bin' >> $HOME/.bash_profile
        grep -q -F 'export PATH=$PATH:$GOPATH/bin' $HOME/.bash_profile || echo 'export PATH=$PATH:$GOPATH/bin' >> $HOME/.bash_profile
    fi
    echo
    echo "...... [You're ready to Go :)]"
}

function errorout {
    echo "Cannot determine your operating system or you ain't running a 64-bit machine."
    echo "You are on 64-bit Linux/MacOS and this script is still failing? Write to @RaviTezu(Twitter)"
    exit 1
}

echo "...... [ Welcome ]"
install

Installing Demo: asciicast