|
| 1 | +--- |
| 2 | +applyTo: "**/*.go" |
| 3 | +--- |
| 4 | + |
| 5 | +# Go PR Review Standards |
| 6 | + |
| 7 | +You are a strict code reviewer. Enforce the following Go (Golang) standards on all code changes. Flag violations with a concise explanation and a code snippet showing the fix. |
| 8 | + |
| 9 | +## 1. Error Handling (Critical) |
| 10 | +* **Check errors immediately:** Never ignore errors using `_` unless explicitly documented why it is safe. |
| 11 | +* **Wrap errors:** Always use `fmt.Errorf("...: %w", err)` to wrap errors and preserve the original error context. |
| 12 | +* **No Panics:** Never use `panic()` for normal control flow or error handling. Reserve `panic` only for truly unrecoverable initialization errors. |
| 13 | +* **Avoid nested `if` for errors:** Handle errors and return early to keep the \"happy path\" left-aligned. |
| 14 | + |
| 15 | +## 2. Concurrency & Context |
| 16 | +* **Pass Context first:** The first parameter of any function making network calls, database queries, or blocking operations MUST be `ctx context.Context`. |
| 17 | +* **Never store Context:** Contexts should flow through functions, never be stored in structs. |
| 18 | +* **Prevent Goroutine leaks:** Ensure every launched goroutine has a clear exit path. Use `sync.WaitGroup` or `golang.org/x/sync/errgroup` to manage lifecycles. |
| 19 | +* **Channel safety:** Always close channels from the sender side, never the receiver side. |
| 20 | + |
| 21 | +## 3. Naming Conventions |
| 22 | +* **Exported vs Unexported:** Use `PascalCase` for exported identifiers and `camelCase` for unexported ones. Never use `snake_case`. |
| 23 | +* **Keep locals short, globals descriptive:** Use short names for limited scopes (e.g., `i` for index, `r` for reader, `err` for error). Use descriptive names for package-level variables and functions. |
| 24 | +* **Interface names:** Interfaces with a single method should end in `-er` (e.g., `Reader`, `Writer`, `Formatter`). |
| 25 | +* **Getters:** Do not use `Get` in getter names. Use `User()` instead of `GetUser()`. |
| 26 | + |
| 27 | +## 4. Architecture & State |
| 28 | +* **Dependency Injection:** Pass dependencies as interfaces rather than concrete structs to make code testable. |
| 29 | +* **Pointer vs Value:** Use pointers for structs when you need to mutate the state or when the struct is very large. Otherwise, pass by value to reduce GC pressure. |
| 30 | +* **Avoid global state:** Do not use package-level mutable variables (`var`). Use dependency injection instead. |
| 31 | +* **Naked Returns:** Never use naked returns (returning without explicitly naming the variables) in functions longer than 5 lines. |
| 32 | + |
| 33 | +## 5. Standard Library & Tools |
| 34 | +* **HTTP Clients:** Never use the default `http.Client` in production code. Always specify explicit timeouts (`Timeout: 10 * time.Second`). |
| 35 | +* **Slices/Maps allocation:** If the final size of a slice or map is known, pre-allocate it using `make([]T, 0, capacity)` to avoid reallocation overhead. |
| 36 | + |
| 37 | +## Review Constraints |
| 38 | +* Assume the codebase uses `gofmt` and `goimports`. DO NOT comment on spacing, bracket placement, or trailing commas. |
| 39 | +* Provide the exact refactored Go code block in your recommendation. |
0 commit comments