How to Automatically Check the Latest Go Version Using a Simple cURL Command
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:
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 thatcurl
runs in "silent" mode, suppressing unnecessary progress information and errors.grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+\.linux-amd64\.tar\.gz'
: The output fromcurl
is piped togrep
, which looks for a specific pattern:goX.Y.Z.linux-amd64.tar.gz
. This regular expression (regex
) matches Go versions that follow the formatgoX.Y.Z
, whereX
,Y
, andZ
are version numbers. The-o
flag tellsgrep
to output only the parts of the input that match the pattern, and the-P
flag allows the use of Perl-compatible regular expressions.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.grep -oP 'go[0-9]+\.[0-9]+\.[0-9]+'
: The result from the firstgrep
is then passed to anothergrep
command to strip away thelinux-amd64.tar.gz
part of the filename, leaving only the version number in the formgoX.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.