Last May I officially introduced versions
, a tool for generating a report of dependencies from different Go-modules-like repositories, I mentioned some ideas I will be adding to versions
really soon like different ways to render the final results (in a form of an image for example) and some extra attributes to add when parsing dependencies (like licenses used and packages with available new releases).
On that new releases topic I realized knowing a way to determine what are the most recent stable Go versions available is also a nice thing to have. The concrete use case for knowing the available Go versions is to determine when to upgrade to a new version or when to know the used version is no longer longer supported.
From a quick search I couldn’t find a way to know this right away, my initial thought was to get that information from the official downloads page in a form of a RSS feed or something similar, but this wasn’t available at the time.
Thankfully gophers-slack exists, I asked in #general and the community shared with me a few different ways to get this information:
TIL (thanks for the Gophers Slack community) 4 ways to know the current/available Go version (to programmatically react to it):
— Mario Carrion (@mariocarrion) May 30, 2020
* https://t.co/BVwvL9bPAb
* https://t.co/FtEkt0oYrH
* https://t.co/26NVYGaSdj
* https://t.co/uFVib44IVh#golang
Using golang.org/VERSION
Visiting https://golang.org/VERSION
renders the most recent stable Go version in HTML, on that page there’s another link for getting a similar result in text format: https://golang.org/VERSION?m=text
.
Using golang.org/dl
Everybody knows https://golang.org/dl/
, the official Go downloads page, but did you know there’s another way to render those results? Using https://golang.org/dl/?mode=json
. This endpoint renders a JSON response that, by default, gives us more details:
- The two most recent and stable versions, and
- Links to the all the different ways to download the compiler, categorized by platform and file type.
But not only that! The official documentation for golang.org/x/website/internal/dl
also supports another query argument for returning more results: https://golang.org/dl/?mode=json&include=all
, this returns a response including a full list of available downloads, including stable, unstable, and archived releases. Pretty neat.
Using github’s atom feed
I didn’t think about this option in the beginning but it definitely makes a lot of sense considering the Go project is also using Github. There’s a way to subscribe the to the Atom feeds for tags (https://github.com/golang/go/tags.atom
) and releases (https://github.com/golang/go/releases.atom
).
This option is a bit similar to the previous long form of https://golang.org/dl/?mode=json&include=all
, it is useful for sure but it requires parsing the actual results in Atom format.
Conclusion
I bet there are more ways to get the available Go versions, like this one using gRPC, but I think the way to go here is to use the semi-official long form version (https://golang.org/dl/?mode=json&include=all
) for this new feature, it includes more details and it is in JSON, so using a combination of net/http
and encoding/json
should suffice.