3 description: Go style rules. Use before you write or review Go code.
10 - Keep Go code in one root `main` package. Do not add `internal/`,
11 `cmd/`, or one-file packages until the user asks for them.
12 - Keep `main.go` limited to the entry point. Name other files for their
13 function. Do not create `main_test.go`.
14 - A file carries its own context. Keep each struct and all of its
15 methods in one file. Give a separate responsibility its own type.
16 - When code in one file gets a user in another file for an unrelated
17 purpose, move it to a file named for its function in the same change.
18 - Start each package-level name that belongs to one feature or
19 integration with the name of that feature. Start the names of its
20 files with the same name. Keep a name generic only when more than one
22 - Write type names with a capital letter, so that a variable can have
23 the name of its type. Write all other package-level names in lower
25 - Write all the letters of an initialism in one case.
26 - Put entry points first, then each struct's methods as a group, then
27 standalone utility functions. Do not put a utility function between
28 the methods of one type.
29 - In each group, order functions by first use in the file.
31 ## Entry point and context
33 - `main` calls `run()` and handles its returned error. Do not use
35 - `run()` creates the SIGINT/SIGTERM signal context and defers its
36 cancellation. Pass that context through operations and let cleanup
37 finish before `main` exits.
38 - Use `ctx` for a context and `cancel` for its cancellation function.
39 Call `defer cancel()` immediately after you create the context.
40 - Reuse `ctx` when a child context replaces its parent. Use a different
41 name only when both contexts stay in use. Return `ctx.Err()` directly.
45 - Handle every returned error.
46 - Reuse an existing `err` for later operations in the same scope. Use a
47 different variable only to keep or join errors.
48 - Use `errors.Is` and `errors.As` to examine errors. Do not compare an
49 error directly with a sentinel, because errors can be wrapped.
50 - When a function has more than one error path, wrap each returned error
51 with the context of its operation. A function with one error path can
52 return the error directly.
53 - When deferred cleanup can fail, use a named `err` result and join the
54 cleanup error with `errors.Join`.
58 - Keep function signatures on one line. Write function bodies on more
59 than one line, also for anonymous functions, deferred cleanup, and
61 - Do not use short `if` initializers.
62 - Use a one-letter receiver name for each method.
63 - Use field names in struct literals. Put each field on its own line
64 when you set more than one field. This includes the cases of a
65 table-driven test. A small type of two fields that acts as one value,
66 such as a key or a coordinate pair, can use a positional literal on
68 - Put an empty line between logical blocks, also after multiline
69 declarations, assignments, literals, and calls. Keep an operation with
70 its immediate error or result check. Keep a short declaration with its
72 - Use GNU-style command flags: two dashes for long flags, such as
73 `--cwd`, and one dash only for one-letter flags, such as `-h`.
74 - Prefer the Go standard library. Prefer Go over cgo.
75 - Do not add compile-time interface assertions. Prove compatibility
76 through actual assignments and calls.
77 - Use `// nolint: name` for lint directives.
81 - After a change to `go.mod` or `go.sum`, update `vendorHash` in
82 `nix/package.nix`. While the module has no dependencies, `vendorHash`
87 - Add tests for meaningful behavior. Do not add tests that only repeat
88 implementation details.
89 - Use `testify/require` for assertions.
90 - Name a table-driven case slice `testCases` and its range variable
91 `tc`. A subtest name describes the behavior, not a generic outcome or
93 - In a test file, put constants immediately after the imports, then the
94 tests, then supporting types, methods, and helpers.
95 - Supply valid required dependencies in tests. Do not make a dependency
96 optional only to make a test simpler.