Go Zero Values And Construction

Guides how a Go value comes into existence honestly — design types whose zero value is already useful (a zero sync.Mutex, bytes.Buffer, or nil slice just works) so you don't write a New that only zeroes fields; use keyed composite literals over positional ones; pick new vs &T{} vs make correctly; reach for functional options only when a type has many optional params; never stash mutable state in package globals; and model enums as typed iota constants that start at one (or reserve zero as an explicit Unknown) with a String() method. Auto-invokes when writing or editing struct construction, New constructors, composite literals, functional options, iota enums/typed constants, Stringer, or on "do I need a constructor", "how should this enum work", or "is this zero value safe". The zero value is part of your API; the compiler does not check enum exhaustiveness.

ctoth ff2fdf1 3 files · 32.3 KB Updated

File contents

ctoth/golang-skills-plugin/tree/main/plugins/golang/skills/go-zero-values-and-construction commit ff2fdf173c

Frequently asked questions

npx skillmds@latest add ctoth/go-zero-values-and-construction