Skip to content

Instantly share code, notes, and snippets.

@marcelopalin
Created April 10, 2024 19:12
Show Gist options
  • Select an option

  • Save marcelopalin/9c0f9c7ab6d56669d68021fc8dee14dc to your computer and use it in GitHub Desktop.

Select an option

Save marcelopalin/9c0f9c7ab6d56669d68021fc8dee14dc to your computer and use it in GitHub Desktop.
Instala ou atualiza o golang na máquina! Cuidado, remove a versão anterior do Golang e instala a nova versão!
#!/bin/bash
# Verifique se curl ou wget está disponível e ajuste o comando de download
DOWNLOAD_CMD=""
if command -v curl &>/dev/null; then
DOWNLOAD_CMD="curl -L" # Adiciona opção -L para seguir redirecionamentos
elif command -v wget &>/dev/null; then
DOWNLOAD_CMD="wget --quiet --continue --show-progress"
else
echo "Este script requer curl ou wget. Instale um deles e tente novamente."
exit 1
fi
# Verifique se tar está disponível
if ! command -v tar &>/dev/null; then
echo "Este script requer tar. Instale tar e tente novamente."
exit 1
fi
# Descubra qual shell está em uso
SHELL_PROFILE=""
if [ "$(basename $SHELL)" = "zsh" ]; then
SHELL_PROFILE=".zshrc"
elif [ "$(basename $SHELL)" = "bash" ]; then
SHELL_PROFILE=".bashrc"
else
echo "Este script suporta Bash e Zsh. Atualize o seu shell e tente novamente."
exit 1
fi
# Verificar e remover a versão pré-existente do Go
if command -v go &>/dev/null; then
OLD_GO_VERSION=$(go version | awk '{ print $3 }')
echo "Found pre-existing Go version $OLD_GO_VERSION. Removing..."
sudo rm -rf /usr/local/go
fi
# Encontre a versão mais recente do Go disponível
echo "Finding latest version of Go for AMD64..."
GO_VERSION=$(curl -s https://go.dev/VERSION?m=text | cut -d' ' -f3 | tr -d 'go')
echo "Downloading latest Go for AMD64: $GO_VERSION"
url="https://dl.google.com/go/go${GO_VERSION}.linux-amd64.tar.gz"
echo "The Go tarball will be downloaded to the current directory."
# Use DOWNLOAD_CMD para o download
if ! $DOWNLOAD_CMD "${url}" -o go.tar.gz; then
echo "Download failed! Unable to download Go from ${url}. Exiting."
exit 1
fi
echo "Extracting File to /usr/local..."
if ! sudo tar -C /usr/local -xzf go.tar.gz; then
echo "Extraction failed! Exiting."
exit 1
fi
echo "Setting Go Paths..."
# Atualização do arquivo de perfil do shell
if ! echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/$SHELL_PROFILE; then
echo "Failed to update ~/$SHELL_PROFILE. Exiting."
exit 1
fi
if ! echo 'export GOPATH=$HOME/go' >> ~/$SHELL_PROFILE; then
echo "Failed to update ~/$SHELL_PROFILE. Exiting."
exit 1
fi
if ! echo 'export PATH=$PATH:$GOPATH/bin' >> ~/$SHELL_PROFILE; then
echo "Failed to update ~/$SHELL_PROFILE. Exiting."
exit 1
fi
echo "Golang $GO_VERSION foi instalado e as variáveis de ambiente definidas!"
echo "Recarregue as variáveis usando o comando: source ~/$SHELL_PROFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment