Skip to content

Instantly share code, notes, and snippets.

@pivaldi
Forked from theaog/update-golang.sh
Last active August 25, 2025 12:40
Show Gist options
  • Save pivaldi/f153b36cf2c9bdeaacd37fbcf2d0a3ea to your computer and use it in GitHub Desktop.
Save pivaldi/f153b36cf2c9bdeaacd37fbcf2d0a3ea to your computer and use it in GitHub Desktop.

Revisions

  1. pivaldi revised this gist Aug 25, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion update-golang.sh
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ $update && {
    curl -OL https://go.dev/dl/"$release_file" || exit 1

    if type go &>/dev/null; then
    goroot=$(go env | grep GOROOT | cut -d'=' -f2 | tr -d '"')
    goroot=$(go env | grep GOROOT | cut -d'=' -f2 | tr -d "\"'")
    [ -z "$goroot" ] && {
    echo "Failed to retrieve last GOROOT env variable"
    exit 1
  2. pivaldi revised this gist Aug 12, 2025. 1 changed file with 45 additions and 16 deletions.
    61 changes: 45 additions & 16 deletions update-golang.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    #!/usr/bin/env bash
    #!/usr/bin/bash

    set -e

    tmp=$(mktemp -d)
    @@ -10,25 +11,53 @@ function cleanup {
    }
    trap cleanup EXIT

    version=$(go version |cut -d' ' -f3)
    release=$(wget -qO- "https://golang.org/VERSION?m=text"| grep go)
    version=$(go version | cut -d' ' -f3)
    release=$(wget -qO- "https://golang.org/VERSION?m=text" | grep -E '^go[1-9]\.[0-9]+\.[0-9]+$')

    [ -z "$release" ] && {
    echo "Failed to retrieve last release"
    exit 1
    }

    release_file="${release}.linux-amd64.tar.gz"
    update=true

    if [[ $version == "$release" ]]; then
    echo "local Go version ${release} is the latest."
    exit 0
    echo "Local Go version ${release} is the latest."
    update=false
    else
    echo "local Go version ${version}, new release ${release} is available."
    echo "Local Go version ${version}, new release ${release} is available."
    fi

    $update && {
    echo "Downloading https://go.dev/dl/$release_file ..."
    curl -OL https://go.dev/dl/"$release_file" || exit 1

    if type go &>/dev/null; then
    goroot=$(go env | grep GOROOT | cut -d'=' -f2 | tr -d '"')
    [ -z "$goroot" ] && {
    echo "Failed to retrieve last GOROOT env variable"
    exit 1
    }

    [ -e "$goroot" ] && {
    echo "removing folder $goroot"
    sudo rm -irf "$goroot"
    }
    else
    goroot=/usr/local/go
    fi

    sudo tar -C "${goroot//go/}" -xzf "$release_file"

    version=$(go version | cut -d' ' -f3)
    echo "local Go version is $version (latest)"

    echo "installing latest Go Tools"
    go install golang.org/x/tools/cmd/goimports@latest
    go install golang.org/x/tools/gopls@latest
    }

    echo "Downloading https://go.dev/dl/$release_file ..."
    curl -OL https://go.dev/dl/"$release_file"

    goroot=$(go env |grep GOROOT |cut -d'=' -f2 |tr -d '"')
    echo "removing folder $goroot"
    sudo rm -rf "$goroot"
    sudo tar -C "${goroot//go}" -xzf "$release_file"

    version=$(go version |cut -d' ' -f3)
    echo "local Go version is $version (latest)"
    # Update binaries installed by "go install" with goroutines.
    go install github.com/nao1215/gup@latest
    gup update
  3. @theaog theaog revised this gist Sep 1, 2023. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions update-golang.sh
    Original file line number Diff line number Diff line change
    @@ -10,12 +10,12 @@ function cleanup {
    }
    trap cleanup EXIT

    version=$(go tool dist version)
    release=$(wget -qO- "https://golang.org/VERSION?m=text")
    version=$(go version |cut -d' ' -f3)
    release=$(wget -qO- "https://golang.org/VERSION?m=text"| grep go)
    release_file="${release}.linux-amd64.tar.gz"

    if [[ $version == "$release" ]]; then
    echo "local Go version ${release} is latest."
    echo "local Go version ${release} is the latest."
    exit 0
    else
    echo "local Go version ${version}, new release ${release} is available."
    @@ -27,8 +27,8 @@ curl -OL https://go.dev/dl/"$release_file"

    goroot=$(go env |grep GOROOT |cut -d'=' -f2 |tr -d '"')
    echo "removing folder $goroot"
    sudo rm -f "$goroot"
    sudo rm -rf "$goroot"
    sudo tar -C "${goroot//go}" -xzf "$release_file"

    version=$(go tool dist version)
    echo "local Go version is $version (latest)"
    version=$(go version |cut -d' ' -f3)
    echo "local Go version is $version (latest)"
  4. @theaog theaog created this gist Jun 8, 2023.
    34 changes: 34 additions & 0 deletions update-golang.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/usr/bin/env bash
    set -e

    tmp=$(mktemp -d)
    pushd "$tmp" || exit 1

    function cleanup {
    popd || exit 1
    rm -rf "$tmp"
    }
    trap cleanup EXIT

    version=$(go tool dist version)
    release=$(wget -qO- "https://golang.org/VERSION?m=text")
    release_file="${release}.linux-amd64.tar.gz"

    if [[ $version == "$release" ]]; then
    echo "local Go version ${release} is latest."
    exit 0
    else
    echo "local Go version ${version}, new release ${release} is available."
    fi


    echo "Downloading https://go.dev/dl/$release_file ..."
    curl -OL https://go.dev/dl/"$release_file"

    goroot=$(go env |grep GOROOT |cut -d'=' -f2 |tr -d '"')
    echo "removing folder $goroot"
    sudo rm -f "$goroot"
    sudo tar -C "${goroot//go}" -xzf "$release_file"

    version=$(go tool dist version)
    echo "local Go version is $version (latest)"