Code-Memo

Modules and packages

Module

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
)
Package

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).

Imports
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 tidy
go get example.com/lib@v1.2.3
go mod tidy
Versioning

Follow semantic import versioning for v2+ (/v2 in module path). Minimal version selection resolves dependencies.

Executable vs library

package main with func main() builds a command; other packages build libraries.

Testing packages

package foo_test is an external test package — black-box testing foo via exported API only.

Workspace (multi-module)

go work ties multiple modules during local development without publishing replace directives.