Index

Updated

July 30, 2026

Quick Reference Guide

Essential Commands

Command Purpose
go run main.go Compile and run
go build Build binary
go test ./... Run tests
go mod tidy Sync dependencies
go fmt ./... Format code
go vet ./... Check for issues

Common Patterns

Pattern Chapter
Error handling Ch 33
Table-driven tests Ch 38
Options pattern Ch 58
Worker pool Ch 40
Context cancellation Ch 43

Type Reference

Type Zero Value
bool false
int 0
string ""
pointer nil
slice nil
map nil

Key Packages

Package Purpose
fmt Formatted I/O
io I/O interfaces
os OS operations
net/http HTTP client/server
encoding/json JSON encoding
sync Synchronization
context Cancellation
testing Test framework

Resources

Worked example

Mini checklist program: type switch + options + timed work.

Save as main.go. Then:

go mod init example
go run .
package main

import (
    "fmt"
    "time"
)

type Opt func(*cfg)
type cfg struct{ n int }

func withN(n int) Opt { return func(c *cfg) { c.n = n } }

func describe(v any) string {
    switch x := v.(type) {
    case string:
        return fmt.Sprintf("string(%d)", len(x))
    case int:
        return fmt.Sprintf("int(%d)", x)
    default:
        return fmt.Sprintf("%T", v)
    }
}

func main() {
    c := cfg{n: 2}
    for _, o := range []Opt{withN(4)} {
        o(&c)
    }
    start := time.Now()
    sum := 0
    for i := 0; i < c.n*100; i++ {
        sum += i
    }
    fmt.Println("describe:", describe("go"))
    fmt.Println("sum:", sum, "took:", time.Since(start) < time.Second)
}

Expected output:

describe: string(2)
sum: 79800 took: true

More examples

Point yourself at the right deep-dive with a tiny table printer.

package main

import "fmt"

func main() {
    rows := [][2]string{
        {"errors.Is/As", "05-errors"},
        {"table tests", "06-testing"},
        {"WaitGroup/select", "07-concurrency"},
        {"httptest", "08-web"},
        {"bench/pprof", "09-performance"},
    }
    for _, r := range rows {
        fmt.Printf("%-18s → %s\n", r[0], r[1])
    }
}

Expected output:

errors.Is/As       → 05-errors
table tests        → 06-testing
WaitGroup/select   → 07-concurrency
httptest             → 08-web
bench/pprof        → 09-performance

Runnable example

Tour of this part in one small program: reflection-ish type switch, a micro “options” knob, and a timed loop you could later benchmark.

Save as main.go. Then:

go mod init example
go run .
package main

import (
    "fmt"
    "time"
    "unsafe"
)

type Config struct {
    Label string
    N     int
}

type Option func(*Config)

func WithN(n int) Option {
    return func(c *Config) { c.N = n }
}

func NewConfig(opts ...Option) Config {
    c := Config{Label: "perf-tour", N: 3}
    for _, o := range opts {
        o(&c)
    }
    return c
}

func describe(v any) string {
    switch x := v.(type) {
    case string:
        return fmt.Sprintf("string len=%d", len(x))
    case int:
        return fmt.Sprintf("int %d", x)
    default:
        return fmt.Sprintf("%T", v)
    }
}

func main() {
    cfg := NewConfig(WithN(5))
    fmt.Println("config:", cfg.Label, cfg.N)
    fmt.Println("describe:", describe(cfg.Label))

    // Layout peek (see unsafe chapter for caveats)
    fmt.Println("sizeof Config:", unsafe.Sizeof(cfg))

    // Tiny workload worth timing / benchmarking later
    start := time.Now()
    sum := 0
    for i := 0; i < cfg.N*1000; i++ {
        sum += i
    }
    fmt.Println("sum:", sum, "took:", time.Since(start).Truncate(time.Microsecond))
}

Expected output: (sizeof and timing vary by arch/machine)

config: perf-tour 5
describe: string len=9
sizeof Config: 24
sum: 12497500 took: 0s

What to notice: This part mixes language escape hatches (reflect/unsafe/cgo), measurement (bench/pprof), and engineering hygiene (design, static analysis, generate, docs). Measure before micro-optimizing.

Try next: Move the loop into sum_test.go as a benchmark with -benchmem. Open related chapters from the table above as you hit each tool.