#!/usr/bin/env bash set -eo pipefail declare -r GO_VERSION=1.18.3 # Hashes are from the table at https://golang.org/dl/ function go_hash () { case $1 in linux_amd64) echo 956f8507b302ab0bb747613695cdae10af99bbd39a90cae522b7c0302cc27245 ;; linux_arm64) echo beacbe1441bee4d7978b900136d1d6a71d150f0a9bb77e9d50c822065623a35a ;; mac) echo a23a24c5528671d444328a36a98056902f699a5a211b6ad5db29ca0c012e0085 ;; windows) echo 9c46023f3ad0300fcfd1e62f2b6c2dfd9667b1f2f5c7a720b14b792af831f071 ;; esac } function install_go_linux () { local -r arch=$(dpkg --print-architecture) ARCHIVE=go${GO_VERSION}.linux-${arch}.tar.gz wget https://golang.org/dl/${ARCHIVE} echo "$(go_hash linux_${arch}) ${ARCHIVE}" | sha256sum --check -- tar -C $1 -xzf ${ARCHIVE} rm ${ARCHIVE} } function install_go_mac () { ARCHIVE=go${GO_VERSION}.darwin-amd64.tar.gz wget https://golang.org/dl/${ARCHIVE} echo "$(go_hash mac) ${ARCHIVE}" | shasum -a 256 --check - tar -C $1 -xzf ${ARCHIVE} rm ${ARCHIVE} } function install_go_windows () { ARCHIVE=go${GO_VERSION}.windows-amd64.zip wget https://golang.org/dl/${ARCHIVE} echo "$(go_hash windows) ${ARCHIVE}" | sha256sum --check -- unzip -qq -d $1 ${ARCHIVE} rm ${ARCHIVE} } function main () { if [[ $# != 1 ]]; then >&2 echo Usage: $0 '' exit 1 fi local -r install_dir=$1 rm -rf "$install_dir" mkdir -p "$install_dir" case $(uname) in Linux) install_go_linux "$install_dir" ;; Darwin) install_go_mac "$install_dir" ;; MSYS_NT*) install_go_windows "$install_dir" ;; *) >&2 echo Error: unknown OS $(uname) exit 1 ;; esac "${install_dir}/go/bin/go" version } main ${@}