Releasing a v2
Semantic Import Versioning showed the consumer's side. This lesson is the author's: you maintain example.com/greet at v1.4.0, the API needs a breaking change, and you want to release v2.0.0 without breaking anyone who stays on v1.
Is it really a v2?
A new major version costs your users a migration, so first check whether you can avoid it:
- Add, don't change. A new function (
HelloAll) next to the old one is a minor release. - Options structs and functional options let functions grow without changing signatures.
- Deprecate the old API with a
// Deprecated:doc comment and keep it working.
Changing a function's signature, removing an exported name, or changing behaviour callers rely on is breaking. That's what v2 is for.
Two ways to lay out a v2
| Major branch | Major subdirectory | |
|---|---|---|
| How | change the module line on the main branch (or a v2 branch) to …/v2 and tag v2.0.0 |
copy the code into a v2/ directory with its own go.mod (module …/v2), tags v2.0.0 on the same branch |
| Repository | one go.mod at the root |
two modules in one repository |
| v1 maintenance | on a v1 branch |
in the root directory |
| Used by | most projects (chi, many others) | projects that need old GOPATH-mode tools to keep working |
Either way, the steps are the same: the module path gets /v2, and every import of the module's own packages must get it too, or v2 code would import v1 packages.
The release, for real
The module has an internal package, so the import rewrite matters. The session publishes v1.4.0 to a local proxy with the gopublish helper from Upgrades, exclude & retract, then makes the classic mistake (tagging v2.0.0 without changing the path), then does it right:
# gopublish DIR MODULE VERSION PROXYDIR: write one module version in GOPROXY layout
mkdir -p ~/tools/gopublish && cat > ~/tools/gopublish/main.go <<'SRC'
package main
import (
"archive/zip"
"os"
"path/filepath"
)
func main() {
dir, mod, ver := os.Args[1], os.Args[2], os.Args[3]
at := filepath.Join(os.Args[4], mod, "@v")
os.MkdirAll(at, 0o755)
gomod, _ := os.ReadFile(filepath.Join(dir, "go.mod"))
os.WriteFile(filepath.Join(at, ver+".mod"), gomod, 0o644)
info := `{"Version":"` + ver + `","Time":"2026-01-15T10:00:00Z"}`
os.WriteFile(filepath.Join(at, ver+".info"), []byte(info), 0o644)
f, _ := os.Create(filepath.Join(at, ver+".zip"))
z := zip.NewWriter(f)
filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
if !d.IsDir() {
rel, _ := filepath.Rel(dir, p)
w, _ := z.Create(mod + "@" + ver + "/" + filepath.ToSlash(rel))
data, _ := os.ReadFile(p)
w.Write(data)
}
return nil
})
z.Close()
f.Close()
list, _ := os.OpenFile(filepath.Join(at, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
list.WriteString(ver + "\n")
list.Close()
}
SRC
(cd ~/tools/gopublish && go mod init gopublish 2>/dev/null && go build -o ~/go/bin/gopublish .)
export GOPROXY=file://$HOME/proxy
mkdir -p greet/internal/join && cd greet
go mod init example.com/greet 2>/dev/null
cat > internal/join/join.go <<'SRC'
package join
import "strings"
func Names(names []string) string { return strings.Join(names, " & ") }
SRC
cat > greet.go <<'SRC'
package greet
import "example.com/greet/internal/join"
// Hello greets one person.
func Hello(name string) string { return "Hello, " + join.Names([]string{name}) }
SRC
cd ..
gopublish greet example.com/greet v1.4.0 proxy
echo "--- the breaking change"
cat > greet/greet.go <<'SRC'
package greet
import "example.com/greet/internal/join"
// Hello greets several people. (v2: was Hello(name string))
func Hello(names ...string) string { return "Hello, " + join.Names(names) }
SRC
echo "--- mistake: tag v2.0.0 without /v2"
gopublish greet example.com/greet v2.0.0 wrong-proxy
mkdir app && cd app
go mod init example.com/app 2>/dev/null
GOPROXY=file://$HOME/wrong-proxy go get example.com/greet@v2.0.0
cd ../greet
echo "--- right: new module path, rewrite own imports"
go mod edit -module example.com/greet/v2
grep -rl 'example.com/greet/' --include='*.go' . | xargs sed -i 's#"example.com/greet/#"example.com/greet/v2/#'
cat go.mod
grep -n import greet.go
go build ./... && go vet ./...
cd ..
gopublish greet example.com/greet/v2 v2.0.0 proxy
find proxy -name list | sort--- the breaking change
--- mistake: tag v2.0.0 without /v2
go: downloading example.com/greet v2.0.0
-> unzip /home/learner/go/pkg/mod/cache/download/example.com/greet/@v/v2.0.0.zip: example.com/greet@v2.0.0: invalid version: should be v0 or v1, not v2
go: unzip /home/learner/go/pkg/mod/cache/download/example.com/greet/@v/v2.0.0.zip: example.com/greet@v2.0.0: invalid version: should be v0 or v1, not v2
--- right: new module path, rewrite own imports
module example.com/greet/v2
go 1.27.3
3:import "example.com/greet/v2/internal/join"
proxy/example.com/greet/@v/list
proxy/example.com/greet/v2/@v/list
The error from the mistake is the go command enforcing the rule at download time: a go.mod that says module example.com/greet can't have version v2.0.0. On the public proxy that version would be unusable for everyone, and since published versions can't be deleted, you would be left explaining it in your README. Check before you tag.
Users of v1 and v2
# gopublish DIR MODULE VERSION PROXYDIR: write one module version in GOPROXY layout
mkdir -p ~/tools/gopublish && cat > ~/tools/gopublish/main.go <<'SRC'
package main
import (
"archive/zip"
"os"
"path/filepath"
)
func main() {
dir, mod, ver := os.Args[1], os.Args[2], os.Args[3]
at := filepath.Join(os.Args[4], mod, "@v")
os.MkdirAll(at, 0o755)
gomod, _ := os.ReadFile(filepath.Join(dir, "go.mod"))
os.WriteFile(filepath.Join(at, ver+".mod"), gomod, 0o644)
info := `{"Version":"` + ver + `","Time":"2026-01-15T10:00:00Z"}`
os.WriteFile(filepath.Join(at, ver+".info"), []byte(info), 0o644)
f, _ := os.Create(filepath.Join(at, ver+".zip"))
z := zip.NewWriter(f)
filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
if !d.IsDir() {
rel, _ := filepath.Rel(dir, p)
w, _ := z.Create(mod + "@" + ver + "/" + filepath.ToSlash(rel))
data, _ := os.ReadFile(p)
w.Write(data)
}
return nil
})
z.Close()
f.Close()
list, _ := os.OpenFile(filepath.Join(at, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
list.WriteString(ver + "\n")
list.Close()
}
SRC
(cd ~/tools/gopublish && go mod init gopublish 2>/dev/null && go build -o ~/go/bin/gopublish .)
export GOPROXY=file://$HOME/proxy
mkdir -p greet && cd greet
go mod init example.com/greet 2>/dev/null
printf 'package greet\n\nfunc Hello(name string) string { return "Hello, " + name }\n' > greet.go
gopublish . example.com/greet v1.4.0 ../proxy
go mod edit -module example.com/greet/v2
printf 'package greet\n\nimport "strings"\n\nfunc Hello(names ...string) string { return "Hello, " + strings.Join(names, " & ") }\n' > greet.go
gopublish . example.com/greet/v2 v2.0.0 ../proxy
cd .. && mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main
import (
"fmt"
"example.com/greet"
greetv2 "example.com/greet/v2"
)
func main() {
fmt.Println(greet.Hello("Ada"))
fmt.Println(greetv2.Hello("Ada", "Grace"))
}
SRC
go mod tidy
go run .
cat go.mod
go list -m -versions example.com/greet example.com/greet/v2go: finding module for package example.com/greet/v2
go: finding module for package example.com/greet
go: downloading example.com/greet v1.4.0
go: downloading example.com/greet/v2 v2.0.0
go: found example.com/greet in example.com/greet v1.4.0
go: found example.com/greet/v2 in example.com/greet/v2 v2.0.0
Hello, Ada
Hello, Ada & Grace
module example.com/app
go 1.27.3
require (
example.com/greet v1.4.0
example.com/greet/v2 v2.0.0
)
example.com/greet v1.4.0
example.com/greet/v2 v2.0.0
go list -m -versions shows two separate version lists: example.com/greet ends at v1.4.0, example.com/greet/v2 starts at v2.0.0. A v1 user who runs go get -u stays on v1.4.0 forever, which is the point.
Release checklist for a major version
- Change the
moduleline (go mod edit -module example.com/greet/v2). - Rewrite the module's own imports to the
/v2path, including tests and examples. go build ./... && go vet ./... && go test ./....- Update docs and the README's
go getline. - Tag
v2.0.0(on the commit with the newgo.mod) and push the tag. - Add
// Deprecated: use example.com/greet/v2to the last v1 release'smoduleline, sogo list -m -utells v1 users. - Keep v1 alive for a while (security fixes on a
v1branch).
gorelease (golang.org/x/exp/cmd/gorelease) compares your API with the previous release and suggests the next version number; Release Engineering covers tagging and changelogs in general.
Test yourself
What must change in go.mod to release v2.0.0 of example.com/greet?
- Nothing, the tag is enough
- The module line becomes
module example.com/greet/v2 go 2.0- A
require example.com/greet v2.0.0line
Why must the module's own imports be rewritten to /v2 too?
- For gofmt
- Otherwise the v2 code would import packages from the v1 module
- Because go.sum requires it
- It is optional
A v1 user runs go get -u ./... after you release v2.0.0. What do they get?
- v2.0.0
- The latest v1.x version
- An error
- Both