~kris/9p

krisyotam.net

krisyotam.net/sites/plan9.krisyotam.com/style/go.md -rw-r--r-- 1.5 KiB
8bfa4b74 — Kris Yotam ci: declare SourceHut source for push builds a month ago

#Go Style

Go has one style. It is enforced by the toolchain.

#gofmt

Run gofmt on everything. There is no debate about brace placement, tab vs spaces, or alignment. The tool decides.

gofmt -w file.go

Or format an entire tree:

go fmt ./...

#Effective Go

The canonical reference is Effective Go. Key points:

  • Naming: MixedCaps, not underscores. Package names are lowercase single words. Interface names end in -er when they have a single method.
  • Semicolons: The lexer inserts them. Opening brace must be on the same line as the control statement.
  • Control flow: if can have an init statement. There is no while -- use for. switch cases do not fall through by default.
  • Error handling: Return errors, do not panic. Check them immediately.
  • Goroutines: Communicate by sharing channels, do not share memory.
  • Commentary: // comments. Doc comments precede declarations with no blank line between.

#Plan 9 Connection

Go was created by Rob Pike, Ken Thompson, and Robert Griesemer. Its toolchain descends from the Plan 9 C compilers. The go command is analogous to Plan 9's mk. The standard library reflects Plan 9 sensibilities: small interfaces, composition over inheritance, explicit error handling.

The style follows directly from Pike's notes on C: short names, no decoration, data structures over algorithms, measure before optimizing.