Develop with go.work
Develop with go.work
A workspace lets several local modules act as main modules for development. go work init creates go.work; use directives list module directories. A workspace changes dependency resolution locally without requiring temporary replace directives in each go.mod. Test individual modules outside the workspace before release.
Use a fresh directory for each example, create the listed files (labels are not file contents), and run the commands in a POSIX shell. Use Go 1.23+, local toolchain, and Python 3 only for the synthetic proxy examples. For isolation, set GOWORK=off, GOTOOLCHAIN=local, GOPROXY=off and GOSUMDB=off for these local labs only; use a fresh GOMODCACHE per lab. Proxy examples override GOPROXY to a local file URL. Do not copy disabled checksum settings to real projects.
--- app/go.mod ---module example.test/app
go 1.23.0
--- app/main.go ---package main
import ( "example.test/lib" "fmt")
func main() { fmt.Println(lib.Version()) }
--- lib/go.mod ---module example.test/lib
go 1.23.0
--- lib/lib.go ---package lib
func Version() string { return "workspace" }
--- Commands (shell, from project root) ---go work init ./app ./libGOWORK="$PWD/go.work" go run ./appGOWORK="$PWD/go.work" go list -mThe app imports lib without a requirement in its go.mod because both modules participate in the workspace. Explicit GOWORK selects the file even when the lab starts with GOWORK=off. This does not make the app independently reproducible.
Expected output:
workspaceexample.test/appexample.test/libAdd a module with go work use
--- app/go.mod ---module example.test/app
go 1.23.0
--- app/main.go ---package main
import ( "example.test/lib" "fmt")
func main() { fmt.Println(lib.Version()) }
--- lib/go.mod ---module example.test/lib
go 1.23.0
--- lib/lib.go ---package lib
func Version() string { return "workspace" }
--- Commands (shell, from project root) ---go work init ./appGOWORK="$PWD/go.work" go work use ./libGOWORK="$PWD/go.work" go run ./appgo work use adds the module directory to the workspace. GOWORK=off disables workspace mode for an independent build. A committed workspace may be appropriate for a deliberately coordinated multi-module repository, but avoid forcing a personal workspace onto consumers.
Expected output:
workspaceTry it locally
Create the two workspace modules with lib returning ready. Initialize a workspace and print the app result.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Modules and workspaces 08: Develop with go.work
Check the lesson objective before moving on.
Which directive lists local modules in go.work?
How do you disable workspace mode for a command?