Last active
April 27, 2024 03:36
-
-
Save jimyag/c9f3933411e881065bc04a956e0134d5 to your computer and use it in GitHub Desktop.
install go from source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
cleanup() { | |
echo "cleanup" | |
rm -rf $HOME/.go | |
} | |
handle_exit() { | |
if [ $? -ne 0 ]; then | |
cleanup | |
exit 1 | |
fi | |
} | |
set_cur_env() { | |
# Add Go binaries to the PATH | |
export PATH="$GOPATH/bin:$GOROOT/bin:$PATH" | |
# Set the Go paths | |
export GOROOT="$HOME/.go/goroot" | |
export GOPATH="$HOME/.go/gopath" | |
export GOROOT_BOOTSTRAP="$HOME/.go/gobootstrap" | |
} | |
# Set up the Go bootstrap | |
gobootstrap(){ | |
version=$(curl -s https://go.dev/dl/\?mode\=json | grep version | head -n 1 | sed 's/.*"version": "\(.*\)".*/\1/') | |
os=$(uname -s | tr '[:upper:]' '[:lower:]') | |
arch=$(uname -m) | |
if [ "$arch" = "x86_64" ]; then | |
arch="amd64" | |
elif [ "$arch" = "aarch64" ]; then | |
arch="arm64" | |
fi | |
filename="${version}.${os}-${arch}.tar.gz" | |
url="https://go.dev/dl/$filename" | |
echo "Downloading Go bootstrap" | |
echo "URL: $url" | |
# Download and extract the Go bootstrap | |
mkdir -p "$HOME/.go" | |
cd "$HOME/.go" | |
wget "$url" | |
tar -zxvf $filename | |
mv "go" "gobootstrap" | |
rm $filename | |
} | |
goroot(){ | |
# Set up the Go root | |
echo "Setting up Go root" | |
git clone https://go.googlesource.com/go "$HOME/.go/goroot" | |
cd "$HOME/.go/goroot" && git checkout "$version" | |
# Build Go from source | |
echo "Building Go from source" | |
cd "$HOME/.go/goroot/src" | |
mkdir -p "$GOPATH" | |
./make.bash | |
} | |
update_env(){ | |
# Update the environment variables | |
echo "Updating environment variables" | |
echo " | |
export GOPATH=\"$HOME/.go/gopath\" | |
export GOROOT_BOOTSTRAP=\"$HOME/.go/gobootstrap\" | |
export GOROOT=\"$HOME/.go/goroot\" | |
export PATH=\"$GOPATH/bin:$GOROOT/bin:\$PATH\" | |
" >> "$HOME/.zshrc" | |
} | |
trap handle_exit EXIT | |
# Set up the Go environment | |
echo "Setting up Go environment" | |
set_cur_env | |
gobootstrap | |
goroot | |
update_env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment