TExcelTAD
Home › Go Modules in Depth › Workspaces: go.work

Workspaces: go.work

A replace ../lib line works, but it lives in go.mod, gets committed by accident, and breaks CI. Since Go 1.18 there is a better tool for working on several modules at once: a workspace. A go.work file lists module directories, and every go command run inside the workspace treats all of them as main modules: imports between them resolve to the local code, with no replace and no edits to any go.mod.

A workspace in one session

Two modules side by side: a library greet and an app that wants a function the library hasn't released yet.

bash · try it yourself
mkdir ws && cd ws && mkdir greet app
(cd greet && go mod init example.com/greet 2>/dev/null)
cat > greet/greet.go <<'SRC'
package greet

import "rsc.io/quote"

func Hello(name string) string { return "Hello, " + name + "! " + quote.Go() }
SRC
(cd greet && go get rsc.io/quote@v1.5.1 2>/dev/null)
(cd app && go mod init example.com/app 2>/dev/null)
cat > app/main.go <<'SRC'
package main

import (
	"fmt"

	"example.com/greet"
	"rsc.io/quote"
)

func main() {
	fmt.Println(greet.Hello("workspace"))
	fmt.Println(quote.Hello())
}
SRC
(cd app && go get rsc.io/quote@v1.5.2 2>/dev/null)
echo "--- without a workspace"
(cd app && go build .)
echo "--- with a workspace"
go work init ./app ./greet
cat go.work
(cd app && go run .)
go list -m
go list -m rsc.io/quote
--- without a workspace
main.go:6:2: no required module provides package example.com/greet; to add it:
	go get example.com/greet
--- with a workspace
go 1.27.3

use (
	./app
	./greet
)
Hello, workspace! Don't communicate by sharing memory, share memory by communicating.
Ahoy, world!
example.com/app
example.com/greet
rsc.io/quote v1.5.2

What the workspace changed:

  • example.com/greet resolved to ./greet although app/go.mod doesn't mention it.
  • go list -m lists both main modules.
  • MVS ran over the combined requirements: greet asks for quote v1.5.1, app for v1.5.2, and the whole workspace uses v1.5.2, one version for everything, as in a single module.
  • The go command finds go.work by looking in the current directory and its parents, so it works from inside app/ too.

go work sync, use and GOWORK=off

bash · try it yourself
mkdir ws && cd ws && mkdir greet app tools
for m in greet app tools; do (cd $m && go mod init example.com/$m 2>/dev/null); done
printf 'package greet\n\nimport "rsc.io/quote"\n\nfunc Hello() string { return quote.Go() }\n' > greet/greet.go
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"example.com/greet"\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(greet.Hello(), quote.Hello()) }\n' > app/main.go
(cd greet && go get rsc.io/quote@v1.5.1 2>/dev/null)
(cd app && go get rsc.io/quote@v1.5.2 2>/dev/null)
go work init ./app ./greet
grep quote greet/go.mod
go work sync
grep quote greet/go.mod
go work use -r .
go work edit -json | grep DiskPath
echo "--- GOWORK=off: build app as CI would, from its go.mod alone"
(cd app && GOWORK=off go build .)
go env GOWORK
	rsc.io/quote v1.5.1 // indirect
require rsc.io/quote v1.5.2
			"DiskPath": "./app"
			"DiskPath": "./greet"
			"DiskPath": "./tools"
--- GOWORK=off: build app as CI would, from its go.mod alone
main.go:6:2: no required module provides package example.com/greet; to add it:
	go get example.com/greet
/home/learner/ws/go.work
Command Effect
go work init ./a ./b create go.work using those modules
go work use ./c / go work use -r . add a module / add every module found below . (and drop missing ones)
go work edit -dropuse ./c, -replace, -json edit or print go.work, like go mod edit
go work sync push the workspace's selected versions back into each module's go.mod
go work vendor vendor the dependencies of the whole workspace
GOWORK=off ignore go.work for one command

go work sync raised greet's requirement to v1.5.2, the version the workspace selected. Use it before releasing modules developed together, so each go.mod alone asks for what was actually tested.

The GOWORK=off build shows the catch: a workspace hides missing requirements. app builds inside the workspace but not on its own, because app/go.mod never required example.com/greet. Before releasing, publish greet, go get example.com/greet@v… in app, and check with GOWORK=off (or in CI, where there is no go.work).

Commit go.work or not?

The Go team's advice: usually don't. A go.work describes your local checkout ("I'm working on these three modules right now"), and committing it makes everyone's builds, and CI, silently ignore the versions in go.mod. Add go.work and go.work.sum to .gitignore.

The exception is a monorepo whose modules are always developed and released together; there a committed go.work is a deliberate choice, and CI should still build each released module with GOWORK=off once.

go.work can hold replace directives too; they apply to the whole workspace and override replace lines in the modules' go.mod files.

Test yourself

What does a go.work file do?

  1. Replaces go.mod
  2. Makes several local modules main modules at once, so imports between them use the local code
  3. Locks dependency versions
  4. Configures the module proxy

In a workspace, greet requires quote v1.5.1 and app requires v1.5.2. Which version does the workspace use?

  1. v1.5.1 for greet and v1.5.2 for app
  2. v1.5.2 for both
  3. v1.5.1 for both
  4. It fails

Why build with GOWORK=off before releasing a module from a workspace?

  1. It is faster
  2. The workspace can hide requirements missing from the module's own go.mod
  3. go.work breaks go.sum
  4. To use the vendor directory