Course Outline: Publishing Go Modules Without Surprises

Go has no central upload step. You publish a module by pushing a version tag to a repository, and the public module proxy fetches it the first time someone asks. That simplicity hides several rules that are permanent once triggered. This outline covers them in four units, with the Go Modules Reference on go.dev as the main reading throughout.

Unit 1: The module path is your name

Learning goal: choose a path you can live with.

The module line in go.mod is both the import path and the location tooling uses to find the code. Changing it later means every importer has to edit their imports.

module example.org/team/package-e

go 1.21

require (
    golang.org/x/text v0.14.0
)

Using a custom domain you control, with a small redirect page, means you can move hosting without changing the path. Using the repository host's path directly is simpler but ties you to it.

Reading: the "Module paths" section of the Go Modules Reference.

Unit 2: Tags are releases

Learning goal: tag correctly the first time.

Exercise: on a throwaway repository, tag a commit, then run go list -m -versions against it from another module.

Unit 3: The v2+ rule

Learning goal: know what changes at a major version.

Go's import compatibility rule says that if an old and a new package have the same import path, the new one must be backwards compatible. So from v2 onward, the major version becomes part of the module path: example.org/team/package-e/v2. You can do this with a /v2 subdirectory or by changing the module line on a dedicated branch. Either way, v1 and v2 can be imported side by side in one program, which is a feature, not a bug.

ApproachGood forWatch out for
Major branch (module line ends in /v2)Most projectsRemember to update internal imports
Major subdirectory (v2/ folder)Supporting old toolchainsDuplicated code to maintain

Unit 4: You cannot unpublish, but you can retract

Learning goal: handle a bad release.

Once the public proxy has cached a version, deleting the tag does not remove it; the checksum database also records its hash, so re-tagging the same version with different content will fail verification for users. Instead, publish a new version whose go.mod contains a retract directive naming the bad one:

retract v1.4.1 // contains a data race in Parse; use v1.4.2

Retracted versions stay downloadable for builds that already pin them, but go get stops selecting them and tools show the comment.

Reading: the "retract directive" section of the Go Modules Reference.

End-of-course checklist

  1. The module path is final and documented.
  2. Your release process creates annotated tags from CI, not by hand.
  3. You know where the /v2 change would go.
  4. You have rehearsed a retraction on a test module.

For the language-neutral versioning ideas behind all of this, see the semver edge cases FAQ.