How to Organize a Monorepo with Go Workspaces

Organize a Go monorepo by creating a go.work file and adding modules with go work use to enable cross-module development.

Create a go.work file in your repository root to link multiple Go modules into a single workspace.

  1. Initialize the workspace file with the default Go version. go work init
  2. Add your local modules to the workspace using their relative paths. go work use ./cmd ./pkg ./internal
  3. Verify the workspace configuration by listing the included modules. go work sync

This setup allows you to run go build or go test across all linked modules simultaneously while resolving local imports correctly.