Golang Style Guide

Per-language style guide for Go. Shared rules: the Coding Style Guide. Requirement levels follow RFC 2119; tags 🌎 / 🏠 are defined there.

1. Formatting 🌎

2. Error Handling 🌎

❌ panic used for an expected failure crashes the caller:

func mustLoad(id string) User {
    u, err := db.Load(id)
    if err != nil {
        panic(err)
    }
    return u
}

βœ… The failure is returned as an error the caller must handle:

func load(id string) (User, error) {
    u, err := db.Load(id)
    if err != nil {
        return User{}, fmt.Errorf("load %s: %w", id, err)
    }
    return u, nil
}

Rationale: errors-as-values is mandated by Go (Go Code Review Comments, Uber, Effective Go). Go's error return is the idiomatic Result (Either).

3. Mutation 🌎 (scoped)

Note: Go idiom permits local mutation; the constraint is on globals and boundary aliasing.

4. Interfaces 🌎

Note: Go idiom is "accept interfaces, return structs" with consumer-defined interfaces (Go Code Review Comments, Effective Go).

References