agentleFS
openshiftopenshift/hypershift/.cursor/rules/100-go-mistakes.mdc

hypershift / rules

Cursor rule551 starsChanged 6 months agoApache-2.0View on GitHub

Go Best Practices

The file

Show its rules and text

We split this 100-go-mistakes.mdc into the rules it gives an agent

37 rules. About 0 are common, 26 are distinctive, and 11 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈26

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 11

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Do not over-nest control flow (e.g., nested if or for blocks).

    line 8
  2. Very specific:

    Avoid init() functions unless absolutely necessary.

    line 9
  3. Distinctive:

    Keep functions small and focused.

    line 10
  4. Distinctive:

    Prefer composition over inheritance (via embedding).

    line 11
  5. Distinctive:

    Use the functional options pattern for constructors where flexibility is needed.

    line 12
  6. Distinctive:

    Avoid defining interfaces until you need them.

    line 16
  7. Distinctive:

    Do not return interfaces from constructors or public APIs.

    line 17
  8. Distinctive:

    Keep interfaces small and focused (generally 1–2 methods).

    line 19
  9. Distinctive:

    Avoid embedding pointer types unless necessary.

    line 23
  10. Distinctive:

    Use value receivers when the method doesn't mutate state or require pointer semantics.

    line 25
  11. Very specific:

    Do not use util, common, or similarly vague package names.

    line 29
  12. Distinctive:

    Avoid package name collisions by using clear, unique names.

    line 30
  13. Distinctive:

    Do not expose unnecessary symbols (keep exported API minimal).

    line 31
  14. Distinctive:

    Avoid memory leaks from slicing large arrays.

    line 36
  15. Distinctive:

    Always check the capacity when copying or appending slices.

    line 37
  16. Distinctive:

    Always initialize maps before use.

    line 42
  17. Very specific:

    Check existence with the two-value assignment (val, ok := m[key]).

    line 43
  18. Distinctive:

    Always check errors — don’t ignore them.

    line 48
  19. Distinctive:

    Avoid panics except in truly unrecoverable cases.

    line 50
  20. Very specific:

    Use errors.Is and errors.As for error comparison in Go 1.20+.

    line 51
  21. Very specific:

    Always defer cancel() when using context.WithCancel.

    line 55
  22. Distinctive:

    Do not leak goroutines — ensure they exit cleanly.

    line 56
  23. Distinctive:

    Avoid data races — use mutexes or channels appropriately.

    line 57
  24. Distinctive:

    Never close a channel from the receiving side.

    line 58
  25. Distinctive:

    Use table-driven tests where possible.

    line 63
  26. Distinctive:

    Avoid global state in tests.

    line 64
  27. Very specific:

    Use t.Helper() in helper functions to improve error tracebacks.

    line 65
  28. Distinctive:

    Keep imports grouped and ordered: stdlib, external, internal.

    line 69
  29. Distinctive:

    Avoid magic numbers — use named constants.

    line 70
  30. Distinctive:

    Prefer explicit over implicit — especially in exported APIs.

    line 71
  31. Distinctive:

    Only use generics when they simplify code or add real flexibility.

    line 75
  32. Distinctive:

    Avoid over-engineering with type parameters.

    line 76
  33. Very specific:

    Avoid using default int unless the size is appropriate.

    line 81
  34. Very specific:

    Avoid precision issues with float32/float64 — prefer math/big if needed.

    line 83
  35. Very specific:

    Prefer any to interface{} in Go 1.18+, but avoid both unless necessary

    line 87
  36. Very specific:

    Do not rely on map or slice comparison — use reflect.DeepEqual or a library.

    line 88
  37. Distinctive:

    Avoid relying on goroutine scheduling or map iteration order.

    line 89

≈ “Common” is an estimate: at least 1 in 5 of the 6,668 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
100-go-mistakes.mdc
---
description: Go Best Practices
globs: *.go,!*.pb.go
alwaysApply: false
---
## General Best Practices
- Avoid variable shadowing.
- Do not over-nest control flow (e.g., nested `if` or `for` blocks).
- Avoid `init()` functions unless absolutely necessary.
- Keep functions small and focused.
- Prefer composition over inheritance (via embedding).
- Use the functional options pattern for constructors where flexibility is needed.

## Interfaces

- Avoid defining interfaces until you need them.
- Do not return interfaces from constructors or public APIs.
- Define interfaces on the consumer side, not the producer side.
- Keep interfaces small and focused (generally 1–2 methods).

## Structs and Methods

- Avoid embedding pointer types unless necessary.
- Don’t overuse getters/setters — prefer public fields when it makes sense.
- Use value receivers when the method doesn't mutate state or require pointer semantics.

## Packages and Imports

- Do not use `util`, `common`, or similarly vague package names.
- Avoid package name collisions by using clear, unique names.
- Do not expose unnecessary symbols (keep exported API minimal).

## Slices

- Distinguish between nil and empty slices.
- Avoid memory leaks from slicing large arrays.
- Always check the capacity when copying or appending slices.
- Preallocate slice capacity when size is known ahead of time.

## Maps

- Always initialize maps before use.
- Check existence with the two-value assignment (`val, ok := m[key]`).
- Be aware that ranging over a map is in random order.

## Error Handling

- Always check errors — don’t ignore them.
- Wrap errors with context when rethrowing.
- Avoid panics except in truly unrecoverable cases.
- Use `errors.Is` and `errors.As` for error comparison in Go 1.20+.

## Goroutines and Concurrency

- Always `defer cancel()` when using `context.WithCancel`.
- Do not leak goroutines — ensure they exit cleanly.
- Avoid data races — use mutexes or channels appropriately.
- Never close a channel from the receiving side.

## Testing and Debugging

- Name tests consistently: `TestXxx`, `BenchmarkXxx`, `ExampleXxx`.
- Use table-driven tests where possible.
- Avoid global state in tests.
- Use `t.Helper()` in helper functions to improve error tracebacks.

## Code Style and Tools

- Keep imports grouped and ordered: stdlib, external, internal.
- Avoid magic numbers — use named constants.
- Prefer explicit over implicit — especially in exported APIs.

## Go 1.18+ Generics

- Only use generics when they simplify code or add real flexibility.
- Avoid over-engineering with type parameters.
- Be cautious with constraint complexity — keep things readable.

## Numbers and Types

- Avoid using default `int` unless the size is appropriate.
- Watch for integer overflows and underflows.
- Avoid precision issues with `float32`/`float64` — prefer `math/big` if needed.

## Miscellaneous

- Prefer `any` to `interface{}` in Go 1.18+, but avoid both unless necessary
- Do not rely on map or slice comparison — use `reflect.DeepEqual` or a library.
- Avoid relying on goroutine scheduling or map iteration order.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.