CodeGym /Cursos /Go SELF /go.work — workspace mode

go.work — workspace mode

Go SELF
Nível 35, Lição 4
Disponível

1. Introduction

When you write an application, it rarely lives in a vacuum. Almost always there is at least one library you are developing alongside it: a shared package with utilities, a separate domain-logic module, an internal SDK, or simply “I moved it nicely into a separate repository.” And then programmers start their favorite game: “why does my application not see the changes in the library when I just wrote them?”

Before workspace mode, the typical path was this: either you publish the changes in the library, which is often too early and inconvenient, or you temporarily add a replace in the application's go.mod pointing to a local folder, and then remember to remove it before publishing. And “remember to” is, as we know, an optimistic scenario.

Workspace mode solves exactly this pain: Go lets you work with several modules at once without forcing you to keep editing each module's go.mod just for local development. The idea is stated very straightforwardly: workspaces let you work with several 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 stop for a moment and agree on a model, otherwise go.work will be perceived as yet another mysterious file that “has to be created somewhere so 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.

Workspace is a mode of operation for Go tools in which they take into account 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 source code directly from disk instead of from a “downloaded version of the dependency.”

From the point of view of the go tool, everything comes down to the fact that it reads go.work and gets three things: the Go version, a list of module directories, and a list of replacements. This is a useful anchor: go.work does not “affect Go syntax”; it affects how the tools resolve dependencies.

Let's 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 via 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 code]

3. The go.work file: how to read it

The go.work file is intentionally made to look like go.mod so 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, the directory where go.mod lives).

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: the module is defined by the go.mod file, and go.work merely says: “treat these modules as local and work with them together.”

4. Example: todoapp + todolib in one workspace

Let's continue our learning line with a task application (the fictional todoapp). Imagine that at some point you decided to move the task model and utilities into a separate module todolib, because “it looks nicer” (and because you are a future library author, not just someone who writes main.go for the rest of your life).

Let's make the structure:

workspace/
  go.work
  todoapp/
    go.mod
    main.go
  todolib/
    go.mod
    task/
      task.go

todolib/go.mod: the library module

// todolib/go.mod
module example.com/todolib

go 1.25

The task package inside todolib

Let's say we have a super-modest function for normalizing a task title. Nothing heroic — but it is clear and practical.

package task

import "strings"

func NormalizeTitle(s string) string {
	return strings.TrimSpace(s)
}

todoapp/go.mod: the application module

// todoapp/go.mod
module example.com/todoapp

go 1.25

require example.com/todolib v0.0.0

The v0.0.0 version 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 the cache, depending on the environment). But if you have a go.work and it says use ./todolib, then Go will take example.com/todolib from the local folder.

And this is where the fun begins: you change NormalizeTitle in todolib, immediately run todoapp — and the application sees the changes right away, without dancing around replace in go.mod.

5. Creating and populating 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 (you can list module directories there), and modules can be added with go work use [moddir] or by editing the file manually. An important detail: a 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 make a “working folder” (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 check it in an application, but without turning one repository into another repository's junk drawer.

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 that workspace mode is enabled

A very typical situation: yesterday everything worked, and today the IDE highlights imports in red, or go test suddenly pulls not the local library, but some version from the network or cache. In moments like that, you want to blame the universe, Mercury retrograde, and your Wi‑Fi neighbor. But it is better to start simple: make sure you are actually in workspace mode.

Go tools rely on the environment variable goWORK: if it points to a file ending in .work, workspace mode is enabled. To find out which go.work is being used, you can run go env GOWORK: if the output is empty, workspace mode is not active.

Why is this important for beginners? Because workspaces are not “part of the code”; they are the “run context.” And run context breaks the easiest: you ran 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 different from what you think.

go work sync: when you need it

The go work sync command sounds like 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 can this be needed? Imagine that in one module you added a new import of an external package, Go pulled the dependency in, but in another workspace module you now need a consistent state. Sometimes tools, IDEs, or checks want the modules' go.mod files to be brought to 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 now I do not understand anything.” Any command that changes go.mod deserves respect. In short: go work sync is useful when you intentionally maintain several modules and want their go.mod files to reflect the state of the workspace, rather than each living its own life.

7. Multi-module development scenarios with go.work

When you first hear “several modules at once,” it sounds like something for big corporations, where each microservice has its own module, each microservice has its own team, and each 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 check 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 source code of todolib instead of the published version. That is exactly why workspaces are being promoted: you can work with several modules at once without needing to use replace in go.mod and then remember to remove it.

Monorepo with multiple modules

If you have a monorepo, then go.work becomes the “assembly point” for local development. You can keep multiple modules while still having a single space where the IDE properly jumps to definitions, and go test ./... in the right context actually sees the whole forest, not just one tree.

Switching dependency configurations

Sometimes you need to check a project in different conditions: “what happens if the library is taken locally?” and “what happens if it is taken as a regular dependency?” Workspaces let you keep different go.work files (in different folders), or even a single go.work where you temporarily comment out or remove use for some modules.

Here's a small table to visually reinforce the difference in approach:

Task replace in go.mod go.work
Quickly “inject” a local version of a dependency 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 entries This is the “native” workspace scenario
Understand why it does not build on a colleague's machine The colleague may not have the same path on disk The colleague may not have a workspace (or may have a different one), but this is easier to diagnose via goWORK

8. Typical mistakes when working with go.work

Mistake #1: adding the module root to use incorrectly, using a package subfolder instead.
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 the 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 picky (although it is simply following the rules literally).

Mistake #2: expecting go.work to “replace” go.mod.
go.work is not a “new go.mod,” but an external context for several modules. Each module still has its own go.mod, and that is its “passport.” The workspace merely tells Go tools: “consider these passports simultaneously.”

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 an “eternal global setting” and forgetting that it affects the build.
go.work is a development tool. It is convenient, but it also changes the context: dependencies may be taken locally. That is great while you are deliberately developing. But if you are running checks, building, or sharing instructions with the team, it is important to understand whether the workspace is being taken into account, and whether it is the same for everyone.

Mistake #5: treating go work sync as “fix everything.”
go work sync is genuinely useful, but it is not magic. It changes the go.mod files of modules, meaning it affects the dependency contract. Use it when you understand what you want to synchronize, not when “something is broken and I pressed the button.”

1
Tarefa
Go SELF, nível 35, lição 4
Bloqueado
Dois módulos
Dois módulos
1
Pesquisa/teste
Módulos Go, nível 35, lição 4
Indisponível
Módulos Go
Módulos Go
Comentários
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION