Curriculum Errors & packaging
Errors & packaging
Go's distinctive error convention — the (T, error) return shape, errors.Is and errors.As for inspection, and the rest of how Go programs are organised into packages and modules.
Go has no exceptions for ordinary control flow. Errors are values
returned alongside results — (T, error). The caller checks
err != nil and decides. This feels noisy at first;…
Inspecting errors without breaking the wrap chain. errors.Is(err,
target) walks the wrap chain looking for a match against a
sentinel. errors.As(err, &target) walks looking for…
v.(T) asserts that an interface value v is actually of type T.
Panics if wrong. Use the comma-ok form to check first:
if t, ok := v.(T); ok { ... }. For multi-branch checks…
Every file declares a package: package foo. Names starting with
a capital letter are exported; lowercase are package-private.
This rule is mechanical and pervasive; once you…
5.5
Modules & go.mod
A module is a versioned collection of packages — your project's
unit of dependency. go mod init github.com/you/yourthing
produces go.mod. go get adds deps. Module paths look…