#!/bin/bash

# Usage:
# bash <(curl -s https://scripts.justman10000.de/installers/go.sh)

version=$(curl -s https://go.dev/dl/ | grep -oP 'go([0-9]+(\.[0-9]+)+)\.linux-amd64\.tar\.gz' | sort -rV | head -n 1 | grep -oP '[0-9]+\.[0-9]+\.[0-9]+')
wget https://go.dev/dl/go$version.linux-amd64.tar.gz
gunzip go$version.linux-amd64.tar.gz
tar xvf go$version.linux-amd64.tar
mkdir /usr/local/go
mv go /usr/local/go/v$version
ln -fs /usr/local/go/v$version/bin/* /usr/bin
rm -r go$version.linux-amd64.tar

cat << EOF >> /root/.bashrc

# Go
export GOPATH=/root/.go/v$version
export GOCACHE=/usr/local/go/v$version/cache
EOF