Go tips
5 tips · newest first
Go tips from recent releases: better error handling, iterators, benchmarks, and stdlib features that replace third-party crutches.
Unwrap errors type-safely with errors.AsType (Go 1.26)
GOerrors.AsType[E] (Go 1.26) is a generic, type-safe version of errors.As. It returns the matched error and a boolean ins…
go1.26errors
@hnoozSandbox filesystem access to a directory with os.Root (Go 1.24)
GOos.Root (Go 1.24) confines all file operations to a directory: Open, Create, Stat, and friends resolve relative to the…
go1.24filesystem
@hnoozWrite custom iterators with range-over-func (Go 1.23+)
GOGo 1.23 lets functions of type iter.Seq[V] be consumed by for range. You return a function that calls yield for each el…
go1.23iterators
@hnoozUse b.Loop() for accurate benchmarks (Go 1.24)
GOb.Loop() (Go 1.24) is the new benchmark loop. Beyond being cleaner than for i := 0; i < b.N; i++, it keeps setup outsid…
go1.24testing
@hnoozTrack tool dependencies in go.mod with tool directives (Go 1.24)
GOGo 1.24 tracks executable dependencies with tool directives in go.mod, retiring the old tools.go blank-import hack. go…
go1.24modules
@hnooz