]> git.giacomolli.uk - onechat/blob - .agents/skills/go-code/SKILL.md
71ff6c9eb749de60864ac6d91f84a2c1c594765f
[onechat] / .agents / skills / go-code / SKILL.md
1 ---
2 name: go-code
3 description: Go style rules. Use before you write or review Go code.
4 ---
5
6 # Go code
7
8 ## Packages and files
9
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
21 feature uses it.
22 - Use unexported names. In the `main` package, an exported name has no
23 effect.
24 - Write all the letters of an initialism in one case.
25 - Put entry points first, then each struct's methods as a group, then
26 standalone utility functions. Do not put a utility function between
27 the methods of one type.
28 - In each group, order functions by first use in the file.
29
30 ## Entry point and context
31
32 - `main` calls `run()` and handles its returned error. Do not use
33 `log.Fatal`.
34 - `run()` creates the SIGINT/SIGTERM signal context and defers its
35 cancellation. Pass that context through operations and let cleanup
36 finish before `main` exits.
37 - Use `ctx` for a context and `cancel` for its cancellation function.
38 Call `defer cancel()` immediately after you create the context.
39 - Reuse `ctx` when a child context replaces its parent. Use a different
40 name only when both contexts stay in use. Return `ctx.Err()` directly.
41
42 ## Errors
43
44 - Handle every returned error.
45 - Reuse an existing `err` for later operations in the same scope. Use a
46 different variable only to keep or join errors.
47 - Use `errors.Is` and `errors.As` to examine errors. Do not compare an
48 error directly with a sentinel, because errors can be wrapped.
49 - When a function has more than one error path, wrap each returned error
50 with the context of its operation. A function with one error path can
51 return the error directly.
52 - When deferred cleanup can fail, use a named `err` result and join the
53 cleanup error with `errors.Join`.
54
55 ## Style
56
57 - Keep function signatures on one line. Write function bodies on more
58 than one line, also for anonymous functions, deferred cleanup, and
59 test callbacks.
60 - Do not use short `if` initializers.
61 - Use a one-letter receiver name for each method.
62 - Use field names in struct literals. Put each field on its own line
63 when you set more than one field. This includes the cases of a
64 table-driven test. A small type of two fields that acts as one value,
65 such as a key or a coordinate pair, can use a positional literal on
66 one line.
67 - Put an empty line between logical blocks, also after multiline
68 declarations, assignments, literals, and calls. Keep an operation with
69 its immediate error or result check. Keep a short declaration with its
70 immediate use.
71 - Use GNU-style command flags: two dashes for long flags, such as
72 `--cwd`, and one dash only for one-letter flags, such as `-h`.
73 - Prefer the Go standard library. Prefer Go over cgo.
74 - Do not add compile-time interface assertions. Prove compatibility
75 through actual assignments and calls.
76 - Use `// nolint: name` for lint directives.
77
78 ## Dependencies
79
80 - After a change to `go.mod` or `go.sum`, update `vendorHash` in
81 `nix/package.nix`. While the module has no dependencies, `vendorHash`
82 is `null`.
83
84 ## Tests
85
86 - Add tests for meaningful behavior. Do not add tests that only repeat
87 implementation details.
88 - Use `testify/require` for assertions.
89 - Name a table-driven case slice `testCases` and its range variable
90 `tc`. A subtest name describes the behavior, not a generic outcome or
91 input.
92 - In a test file, put constants immediately after the imports, then the
93 tests, then supporting types, methods, and helpers.
94 - Supply valid required dependencies in tests. Do not make a dependency
95 optional only to make a test simpler.