A module is a collection of packages versioned together, declared in go.mod at the module root.
module example.com/myapp
go 1.22
require (
golang.org/x/sync v0.6.0
)
Each directory of .go files with the same package clause is one package. The package name is usually the last path element (http in net/http).
import (
"fmt"
"net/http"
"example.com/myapp/internal/auth"
)
internal subdirectories are visible only to modules rooted inside the parent tree — use for app-private code.
go get, go mod tidygo get example.com/lib@v1.2.3
go mod tidy
Follow semantic import versioning for v2+ (/v2 in module path). Minimal version selection resolves dependencies.
package main with func main() builds a command; other packages build libraries.
package foo_test is an external test package — black-box testing foo via exported API only.
go work ties multiple modules during local development without publishing replace directives.