Don't check in go.work

Here is a quick tip as I have recently seen yet another repo with go.work and go.work.sum files in it.

Don't check in your go.work files to source control.

go.work is meant to specify a local workspace, in which you can redirect imports of remote packages to local repos. Those redirects are specific to your local file system. You're totally free in deciding about where to clone repos to, and this defines the contents of your go.work file.

If you check in the go.work file, your go.work files gets used on a machine that don't have the same Go workspace structure as your machine. The redirects in go.work thus will fail at their machines.

So always add go.work* to your .gitignore file (or the equivalent for the source control system you are using).

The go mod documentation lists two more cases against checking in go.work:

  1. A developer who checks out your repository might have their own go.work file in a parent directory. The checked-in file would then overwrite the one in the parent directory.
  2. Continuous Integration (CI) tools might select the wrong version of a dependency for testing if a go.work file is present.

A tip inside the tip

If you really want to share your workspace layout, use a go.work.example file for that purpose and advise the repo users to copy this file to go.work if they want and adapt it as needed.