Appendix A: Reference Summary
A compact reminder of the language, not a specification. The Go Programming Language Specification at https://go.dev/ref/spec is short enough to read in an evening and is the authority.
Keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Twenty-five, and that is the list. Everything else you see is a predeclared identifier, not a keyword, and can be shadowed.
Predeclared identifiers
Types bool byte complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr
any comparable
Constants true false iota
Zero nil
Functions append cap clear close complex copy delete imag len
make max min new panic print println real recover
Declarations
var x int zero value
var x = v inferred type
var x int = v both
x := v short form, inside functions only
const K = v compile-time constant
type T U new type with U's representation
type T = U alias for U
func f(a, b int) error function
func (r T) m() method on T
Zero values
0 numeric types
false bool
"" string
nil pointer, slice, map, channel, func, interface
A struct’s zero value has every field at its own zero value. An array’s has every element at its own.
Composite types
[N]T array, length is part of the type
[]T slice
map[K]V map, K must be comparable
chan T channel; <-chan T receive-only, chan<- T send-only
*T pointer
func(A) B function
struct{...} struct
interface{...} interface
make([]T, len, cap) make(map[K]V) make(chan T, buf)
new(T) &T{...}
T{a, b} positional literal
T{Field: a} keyed literal
Operators, tightest binding first
5 * / % << >> & &^
4 + - | ^
3 == != < <= > >=
2 &&
1 ||
Unary: + - ! ^ * & <-. There is no ?:, no **, no operator overloading, and
++ and -- are statements rather than expressions.
Control flow
if cond { } else if cond { } else { }
if init; cond { }
for init; cond; post { }
for cond { }
for { }
for i, v := range collection { }
for i := range n { }
switch v { case a, b: ... default: ... }
switch { case cond: ... }
switch v := x.(type) { case T: ... }
select { case <-ch: ... case ch <- v: ... default: ... }
break continue goto fallthrough return defer go
Labels attach to for, switch, and select, and break/continue may name
one.
Format verbs
%v default format %+v with struct field names
%#v Go syntax %T the type
%d integer %b %o %x %X base 2, 8, 16
%f decimal float %e exponent %g whichever is shorter
%s string %q quoted %c the rune
%t bool %p pointer %% a literal percent
Width and precision go between: %6.2f, %-10s left-justified, %08d
zero-padded.
Frequently used packages
fmt formatted I/O
os files, arguments, environment, exit
strings search, split, replace, Builder
strconv text to and from numbers
bytes the strings operations, on []byte
bufio buffered I/O and Scanner
io Reader, Writer, Copy, MultiReader
errors New, Is, As, Join
slices Sort, Contains, Index, Equal, SortFunc
maps Keys, Values, Clone
sort the older sorting interface
time durations, instants, timers
sync WaitGroup, Mutex, Once
context cancellation and deadlines
encoding/json Marshal and Unmarshal
net/http client and server
testing T, B, and the test runner
flag command-line flags
math, math/rand, regexp, path/filepath, log
go doc <pkg> prints any of them.
The go command
go run ./pkg build and run
go build ./... build
go install ./cmd/x build into ~/go/bin
go test ./... test -v -run -cover -race -bench
go fmt ./... format
go vet ./... report likely mistakes
go doc pkg.Name documentation
go mod init <path> start a module
go mod tidy reconcile dependencies
go get pkg@version add or update one
go clean -cache discard the build cache
Naming
Capitalized names are exported from their package; the rest are not. Packages
are one lower-case word. Acronyms keep their case, so HTTPServer and userID.
Getters drop the get, so it is u.Name() rather than u.GetName(). A
one-method interface is the method plus -er: Reader, Stringer, Formatter.
Receiver names are one or two letters and the same on every method of a type.
Short names are expected for short lives, so i, n, err, w, buf.