1. Introduction
When you write an application, it rarely lives in a vacuum. Almost always there is at least one library that you develop alongside it: a shared utilities package, a separate domain-logic module, an internal SDK, or simply something you “moved into a separate repository to keep things tidy.” And that is where programmers’ favorite game begins: “why does my application not see the changes in the library, even though I just wrote them?”
Before workspace mode, the typical path was this: either you publish the changes in the library first (which is often too early and inconvenient), or you temporarily add a replace directive in the application’s go.mod pointing to a local folder, and then remember to remove it before publishing. And “remember to remove it” is, as we know, the optimistic scenario.
Workspace mode solves exactly this pain point: Go lets you work with several modules at the same time without forcing you to keep editing each module’s go.mod for local development. The idea is stated very directly: workspaces let you work with multiple modules simultaneously without needing to edit go.mod for each module, while each module inside the workspace is treated as “main” when resolving dependencies.
2. What workspace mode is: the mental model
It is important to pause for a moment and agree on a model, otherwise go.work will feel like yet another mysterious file that “somewhere needs to be created so that it works.” We do not do that: we are not shamans, we are engineers… or at least we try to look like engineers in the computer’s eyes.
A workspace is a Go tool mode in which the tools consider not one “main module” but a set of local modules listed in go.work. As a result, when one module imports packages from another module, Go can take their sources directly from disk instead of from a “downloaded dependency version.”
From the point of view of the go tool, everything comes down to reading go.work and getting three things: the Go version, the list of module directories, and the list of replacements. This is a useful anchor: go.work does not “affect Go syntax”; it affects how tools resolve dependencies.
Let us draw a simple diagram:
flowchart TD
A[You run: go test / go run / go build] --> B[Go looks for context]
B --> C{Workspace mode enabled?}
C -->|no| D[Work as usual: one main module by go.mod]
C -->|yes| E[Read go.work]
E --> F[use: list of modules on disk]
F --> G[Imports between these modules are taken from local source files]
3. The go.work file: how to read it
The go.work file is deliberately made to look similar to go.mod so that the brain does not overheat. It has directives, and the most important one for us is use.
go.work contains the directives go, use, and, if needed, replace. For this lecture, the most important thing is to understand use: it adds a module on disk to the workspace (that is, a directory containing go.mod).
An example of a minimal go.work for Go 1.25:
// go.work
go 1.25
use (
./todoapp
./todolib
)
There are no imports or “package paths” here — only module directories. And that makes sense: a module is defined by the go.mod file, while go.work merely says: “treat these modules as local and work with them together.”
4. Example: todoapp + todolib in one workspace
Let us continue our learning line with a task application (the fictional todoapp). Imagine that at some point you decide to move the task model and utilities into a separate todolib module because “it looks nicer that way” (and because you are a future library author, not just someone who writes main.go until the end of their life).
Let us build the structure:
workspace/
go.work
todoapp/
go.mod
main.go
todolib/
go.mod
task/
task.go
todolib/go.mod: library module
// todolib/go.mod
module example.com/todolib
go 1.25
The task package inside todolib
Let us say we have a very modest title-normalization function for tasks. Nothing heroic — but clear and practical.
package task
import "strings"
func NormalizeTitle(s string) string {
return strings.TrimSpace(s)
}
todoapp/go.mod: application module
// todoapp/go.mod
module example.com/todoapp
go 1.25
require example.com/todolib v0.0.0
The version v0.0.0 here is just a placeholder for the example (in reality you would choose a specific version or work differently). What matters now is the idea: the application depends on the library module.
Importing the library in todoapp/main.go
package main
import (
"fmt"
"example.com/todolib/task"
)
func main() {
title := task.NormalizeTitle(" buy milk ")
fmt.Println(title) // buy milk
}
If you run todoapp without a workspace, Go will try to find the dependency example.com/todolib as a module (download it or take it from cache, depending on the environment). But if you have a go.work file and it contains use ./todolib, then Go will take example.com/todolib from the local folder.
And this is where the joy begins: you change NormalizeTitle in todolib, then immediately run todoapp — and the application sees the changes right away, without juggling replace in go.mod.
5. Creating and filling a workspace
When you first encounter workspaces, a practical question arises: “Okay, but how do I actually set this up?” You can create go.work manually, or you can ask the Go tools to do it for you.
The basic path is this: a workspace is created with the go work init command (optionally with a list of module directories), and modules can be added via go work use [moddir] or by editing the file manually. An important detail: the workspace does not have to physically contain the modules you work with. In other words, go.work is a “map,” not a “container.”
If you have a monorepo, it is convenient to live like this:
repo/
go.work
services/
todoapp/ (go.mod)
importtool/ (go.mod)
libs/
todolib/ (go.mod)
textkit/ (go.mod)
And if you have several repositories next to each other on disk, you can create a “workspace directory” that contains only go.work, while the modules are located in neighboring directories. This is especially nice when you want to fix a bug in a library and immediately verify it in the application, without turning one repository into another’s dump.
Another practical option is recursive module addition. There is a go work use -r . variant that recursively adds directories containing go.mod. This is convenient if you have many modules in a directory tree and do not want to list them manually.
6. Diagnosing and maintaining a workspace
How to tell whether workspace mode is enabled
A very typical situation: “everything worked yesterday,” but today the IDE highlights imports in red, or go test suddenly pulls not the local library, but some version from the network or cache. At such moments you want to blame the universe, retrograde Mercury, and the neighbor on Wi‑Fi. But it is better to start with something simple: make sure you are actually in workspace mode.
Go tools rely on the goWORK environment variable: if it points to a file ending with .work, workspace mode is enabled. To find out which go.work is used, you can run go env GOWORK: if the output is empty, workspace mode is not active.
Why is this especially important for beginners? Because workspaces are not “part of the code”; they are “run context.” And run context breaks most easily: you launched the command from the wrong folder, opened the wrong root in the IDE, the terminal is looking at another project — and now you are debugging something other than what you think you are.
go work sync: when you need it
The go work sync command sounds as if it should fix life, relationships, and dependencies. In practice, it does something more specific: it “pushes dependencies from go.work back into the go.mod files of workspace modules.”
When might that be needed? Imagine that in one module you added a new external package import, Go fetched the dependency, but in another module of the workspace you now need a consistent state. Sometimes tools, IDEs, or checks want the modules’ go.mod files to be brought into the expected form. Then sync helps bring everything into a consistent state.
But it is important not to think of sync as “press it and I no longer understand anything.” Any command that changes go.mod deserves respect. In short: go work sync is useful when you consciously manage multiple modules and want their go.mod files to reflect the workspace state rather than living separate lives.
7. Multi-module development scenarios with go.work
When you first hear “multiple modules at once,” it sounds like something for big corporations where every microservice has its own module, every microservice has its own team, and every team has its own chat where they discuss who broke the build. In reality, go.work is useful in small projects too; the scenarios are just simpler there.
“I edit the library and immediately test it in the application”
This is the most common scenario. You make a change in todolib, and then you run todoapp. In workspace mode, the application uses the local todolib sources instead of the published version. This is exactly why workspaces are promoted: you can work with several modules at once without needing to use replace in go.mod and then remember not to forget deleting it.
A monorepository with several modules
If you have a monorepo, then go.work becomes the “assembly point” for local development. You can keep several modules, while still having a unified space where the IDE can jump between definitions correctly, and go test ./... in the right context really sees the whole forest, not just one tree.
Switching dependency configurations
Sometimes you need to check a project under different conditions: “what if the library is taken locally?” and “what if it is taken as a regular dependency?” Workspaces let you keep different go.work files (in different folders), or even one go.work where you temporarily comment out or remove use for some modules.
A small table to visually reinforce the difference between the approaches:
| Task | replace in go.mod | go.work |
|---|---|---|
| Quickly “inject” a local dependency version into one module | Works, but you must remember to remove it | No need to edit modules’ go.mod files for local development |
| Develop 2–5 modules at the same time and run tests together | Often turns into a set of temporary replace directives | This is the “native” workspace scenario |
| Figure out why a colleague cannot build | The colleague may not have the same path on disk | The colleague may not have a workspace (or may have a different one), but that is easier to diagnose through goWORK |
8. Typical mistakes when working with go.work
Mistake #1: adding the module root to use incorrectly, as a package subfolder.
It is very tempting to write use ./todolib/task because you need the task package. But workspace works at the module level, and a module is defined by a go.mod file. Therefore, use must contain the directory where go.mod lives. Otherwise you will get strange symptoms: imports do not resolve, the IDE does not understand the structure, and you start suspecting that Go is just being stubborn (although it is only literally following the rules).
Mistake #2: expecting go.work to “replace” go.mod.
go.work is not a “new go.mod”; it is an external context for multiple modules. Each module still has its own go.mod, and that is its “passport.” The workspace merely tells Go tools: “consider these passports at the same time.”
Mistake #3: not checking whether workspace mode is enabled, and debugging the wrong reality.
If goWORK does not point to a .work file, workspace mode is not active, and Go will work “as usual.” Checking go env GOWORK (empty or not empty) often saves hours of life and a couple of nerve cells.
Mistake #4: treating go.work as a “permanent global setting” and forgetting that it affects builds.
go.work is a development tool. It is convenient, but it also changes the context: dependencies may be taken locally. That is great as long as you are consciously developing. But if you run checks, builds, or share instructions with the team, it is important to know whether the workspace is taken into account, and whether everyone has the same one.
Mistake #5: treating go work sync as “fix everything.”
go work sync is truly useful, but it is not magical. It changes the go.mod files of modules, which means it affects the dependency contract. Use it when you understand what you want to synchronize, not when “something does not work and I pressed a button.”
GO TO FULL VERSION