golang-pitfalls-code-organization
Installation
SKILL.md
Golang Pitfalls: Code & Project Organization
Source material: mistakes #1-16 from 100 Go Mistakes and How to Avoid Them (teivah/100-go-mistakes).
Apply these rules when writing Go code related to code structure, packages, interfaces, and project layout. The number in each heading maps to the corresponding mistake in the book.
1. Unintended variable shadowing (#1)
- Variable shadowing (redeclaring a name in an inner block) can make code compile while assigning to a different variable than expected.
- Avoid shadowing; run
go vetor a shadowing linter to detect it. Only reusing idiomatic names likeerris occasionally acceptable. - Prefer unique names in inner scopes.
2. Unnecessary nested code (#2)
- Reduce nesting; keep the happy path aligned left and return early.
- If an
ifblock returns, drop theelse. - Flip conditions so non-happy paths return early: