Use GOTOOLCHAIN to run older versions of the Go toolchain

I've been working through Mastering Distributed Tracing to learn more about (you guessed it) tracing. The book is from 2019, and while I've been assured that its contents are still sufficiently up-to-date, the Go-based code examples are a bit dated; namely, they use dep for dependency management, rather than the new(er) built-in module system. dep seems like a fine tool that had its time, but seeing as I'm used to Go modules, and I don't really want to learn an obsolete tool just for some book exercises, I created a Go module for the exercises and started converting the Gopkg.toml/Gopkg.lock files by hand.

Fortunately, I quickly thought "hey, there must be a tool that automates this - right?"

Well, it turns out that go mod init does!

If a configuration file for a vendoring tool is present, init will attempt to import module requirements from it. init supports the following configuration files.
...
Gopkg.lock (dep)
...

...or at least it used to, before Go 1.22.

Now, it seems that the feature was removed because it wasn't exactly perfect - I totally understand that decision, but having it would really give me a leg up here! Wouldn't it be nice if I could run an older version of go mod init, just for this purpose?

If this were another language, I would use something like rustup, or pyenv - or maybe I would just use downgrade to temporarily downgrade my system's Go installation to the latest 1.21 release. These aren't terrible options, but Go actually makes this even easier via GOTOOLCHAIN - it was just a matter of running GOTOOLCHAIN=go1.21.13 go mod init github.com/PacktPublishing/Mastering-Distributed-Tracing/Chapter04/go, and that gave me a go.mod that I only needed to tweak a little to get going!

(By the way, if you're interested in the book, here's my fork with the go.mod changes: https://github.com/hoelzro/Mastering-Distributed-Tracing)

Published on 2025-02-16