How to Automatically Check the Latest Go Version Using a Simple cURL Command

How to Automatically Check the Latest Go Version Using a Simple cURL Command
Photo by Maarten Scheel / Unsplash

In this article, we'll walk you through a simple way to automatically check the latest stable Go version directly from the official Go download page. We'll do this using a combination of curl and grep commands, which fetch and filter the version information from the website. Here's how it works.

Command

Below is the complete command that checks and retrieves the latest stable version of Go for the Linux amd64 architecture:

curl -s https://go.dev/dl/ | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz' | head -n 1 | grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+'

Output

go1.23.1

Let's break down each part of the command:

  1. curl -s https://go.dev/dl/: This part sends an HTTP request to the Go download page and retrieves the raw HTML content of the page. The -s flag ensures that curl runs in "silent" mode, suppressing unnecessary progress information and errors.
  2. grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz': The output from curl is piped to grep, which looks for a specific pattern: goX.Y.Z.linux-amd64.tar.gz. This regular expression (regex) matches Go versions that follow the format goX.Y.Z, where X, Y, and Z are version numbers. The -o flag tells grep to output only the parts of the input that match the pattern, and the -P flag allows the use of Perl-compatible regular expressions.
  3. head -n 1: Since there may be multiple version entries listed, this command ensures that only the first match (typically the latest stable release) is selected. head outputs the first line of the results.
  4. grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+': The result from the first grep is then passed to another grep command to strip away the linux-amd64.tar.gz part of the filename, leaving only the version number in the form goX.Y.Z.

This approach allows you to automate checking for new versions of Go in your scripts or as part of your update routines.

Finally

By using this simple combination of curl, grep, and head, you can quickly and efficiently check the latest Go version available for download. This method is especially useful for automation in CI/CD pipelines or for anyone who wants to stay up-to-date with the latest Go releases without manually visiting the Go website.

Hope it helps.

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe