Workspace
A workspace is a go.work file that tells the toolchain to use several local modules together as if they were already required at the versions on disk. It is a development overlay, not something you publish.
Detailed Explanation
You are changing a library and an application in the same week. The app's go.mod still says require example.com/lib v1.4.2. Until you publish a new tag, go test in the app will keep compiling the old module from the cache. A workspace lists both module directories so builds resolve example.com/lib to ../lib automatically, for every command you run from that workspace.
go.work is the file. It lives above or beside the modules it names:
go 1.22
use (
./shop
./lib
)
go work init ./shop ./lib writes it. go work use ./other adds a line. The toolchain walks up from the current directory to find go.work, the same way it walks up to find go.mod.
Workspaces do not replace modules. Each module still has its own go.mod, its own versions, its own tests. The workspace only changes how those modules find each other while you are editing. Downstream users who clone only shop never see your go.work. That is why you usually add go.work to .gitignore in a repo that is one module, and commit it in a monorepo where the layout is the product.
MVS still applies across the workspace. If shop requires lib v1.4.2 and the local lib is a different major, you still have a version problem. Workspaces solve "I want this checkout," not "these two modules disagree about API." For a temporary one-module hack, replace in go.mod is enough. Use a workspace when several modules must see one another, or when you do not want to commit a replace.
go.work.sum appears next to go.work. It records hashes for the extra graph the workspace needs. Commit both when the workspace is shared; ignore both when it is personal.
What it costs. A forgotten go.work on a laptop is a common "why is CI green and my machine red" story: locally you compile ../lib, CI compiles the tagged version. The opposite also happens — you fix a bug in lib, forget the workspace, and wonder why the app still fails. go env GOWORK prints the active file. GOWORK=off disables it for one command.
Code Examples
# From a directory that contains shop/ and lib/:
go work init ./shop ./lib
# go.work
# go 1.22
# use (
# ./shop
# ./lib
# )# See which workspace file is active.
go env GOWORK
# Build the app as if no workspace existed (uses tagged requires).
GOWORK=off go test ./shop/...package main
import (
"fmt"
"example.com/lib/tokens"
)
func main() {
// Inside a workspace, this import compiles against ./lib
// even if shop/go.mod still requires an older tagged version.
fmt.Println(tokens.New())
}Common Gotchas
A local `go.work` hides the versions CI will use
if you do not commit the workspace, remember that `go test` on your laptop is not the same graph as `go test` in the pipeline. Run `GOWORK=off go test ./...` before you push a library bump.
Workspaces do not invent a module
every `use` path must already contain a `go.mod`. `go.work` cannot turn a folder of files into a module.
Two `use` entries that claim the same module path conflict
you cannot put two checkouts of `example.com/lib` in one workspace. Pick one directory.
`replace` in a member module still applies
the workspace adds local modules; it does not erase that module's own `go.mod` directives.