Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active October 6, 2023 18:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save n8henrie/1043443463a4a511acf98aaa4f8f0f69 to your computer and use it in GitHub Desktop.
Save n8henrie/1043443463a4a511acf98aaa4f8f0f69 to your computer and use it in GitHub Desktop.
Download and install Golang for Raspberry Pi
go*.linux-armv6l.tar.gz
#! /bin/bash
# [get_golang.sh](https://gist.github.com/n8henrie/1043443463a4a511acf98aaa4f8f0f69)
# Download latest Golang release for ARM
set -euf -o pipefail
download_latest_go() {
local GOURLREGEX='https://.*?/go/go[0-9\.]+\.linux-armv6l.tar.gz'
echo "Finding latest version of Go for ARM..."
local url_and_hash="$(wget -qO- https://golang.org/dl | grep --perl-regexp --after-context=5 --max-count=1 "${GOURLREGEX}" )"
local url="$(echo "$url_and_hash" | grep -oP ${GOURLREGEX})"
local hash="$(echo "${url_and_hash}" | grep -oP '(?<=<tt>)\w+(?=</tt>)')"
latest="${url##*/go/}"
if [ -f "${latest}" ]; then
echo "File ${latest} exists. Checking against hash: ${hash}"
if [ "$(sha256sum <"${latest}")" == "${hash} -" ]; then
echo "Hash matches"
else
echo "Hash mismatch. Please remove the local file and re-run"
fi
else
echo "Downloading latest Go for ARM: ${latest}"
wget --quiet --continue --show-progress "${url}"
fi
}
remove_old_go() {
while true; do
read -n 1 -p 'Delete old Go from `/usr/local/go`? [Y/n]: ' rm_oldgo
echo
case "${rm_oldgo}" in
Y|y) sudo rm -rf /usr/local/go; break ;;
N|n) break ;;
*) : ;;
esac
done
}
install_new_go() {
while true; do
read -n 1 -p 'Install new Go to `/usr/local/go`? [Y/n]: ' install_newgo
echo
case "${install_newgo}" in
Y|y) sudo tar -C /usr/local -xzf "${latest}"; break ;;
N|n) break ;;
*) : ;;
esac
done
}
remove_download() {
while true; do
read -n 1 -p 'Remove downloaded tarball? [Y/n]: ' rm_tarball
echo
case "${rm_tarball}" in
Y|y) rm -f "${latest}"; break ;;
N|n) break ;;
*) : ;;
esac
done
}
echo "Starting with $(go version 2> /dev/null || echo "go not installed")"
download_latest_go
remove_old_go
install_new_go
remove_download
echo "Ending with $(go version)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment