Skip to content

Instantly share code, notes, and snippets.

@dancompton
Created July 11, 2018 20:29
Show Gist options
  • Save dancompton/10e6c0d8ab085c7d407b9da417b5f3ef to your computer and use it in GitHub Desktop.
Save dancompton/10e6c0d8ab085c7d407b9da417b5f3ef to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
VERSION="1.9.3"
if [ -n "`$SHELL -c 'echo $ZSH_VERSION'`" ]; then
# assume Zsh
shell_profile="zshrc"
elif [ -n "`$SHELL -c 'echo $BASH_VERSION'`" ]; then
# assume Bash
shell_profile="bashrc"
fi
DFILE="go$VERSION.linux-386.tar.gz"
if [ "$1" == "--32" ]; then
DFILE="go$VERSION.linux-386.tar.gz"
elif [ "$1" == "--64" ]; then
DFILE="go$VERSION.linux-amd64.tar.gz"
elif [ "$1" == "--arm" ]; then
DFILE="go$VERSION.linux-armv6l.tar.gz"
elif [ "$1" == "--darwin" ]; then
DFILE="go$VERSION.darwin-amd64.tar.gz"
elif [ "$1" == "--remove" ]; then
rm -rf "$HOME/.go/"
sed -i '/# GoLang/d' "$HOME/.${shell_profile}"
sed -i '/export GOROOT/d' "$HOME/.${shell_profile}"
sed -i '/:$GOROOT/d' "$HOME/.${shell_profile}"
sed -i '/export GOPATH/d' "$HOME/.${shell_profile}"
sed -i '/:$GOPATH/d' "$HOME/.${shell_profile}"
echo "Go removed."
exit 0
if [ -d "$HOME/.go" ] || [ -d "$HOME/go" ]; then
echo "The 'go' or '.go' directories already exist. Exiting."
exit 1
fi
echo "Downloading $DFILE ..."
wget https://storage.googleapis.com/golang/$DFILE -O /tmp/go.tar.gz
if [ $? -ne 0 ]; then
echo "Download failed! Exiting."
exit 1
fi
echo "Extracting File..."
tar -C "$HOME" -xzf /tmp/go.tar.gz
mv "$HOME/go" "$HOME/.go"
touch "$HOME/.${shell_profile}"
{
echo '# GoLang'
echo 'export GOROOT=$HOME/.go'
echo 'export PATH=$PATH:$GOROOT/bin'
echo 'export GOPATH=$HOME/go'
echo 'export PATH=$PATH:$GOPATH/bin'
} >> "$HOME/.${shell_profile}"
mkdir -p $HOME/go/{src,pkg,bin}
echo -e "\nGo $VERSION was installed.\nMake sure to relogin into your shell or run:"
echo -e "\n\tsource $HOME/.${shell_profile}\n\nto update your environment variables."
echo "Tip: Opening a new terminal window usually just works. :)"
rm -f /tmp/go.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment