Go Coding Standards
This skill provides modern Go coding guidelines and best practices for this project.
When to Apply
Apply these standards when:
- Writing new Go code
- Reviewing or refactoring existing Go code
- Designing package APIs and interfaces
- Implementing error handling strategies
Core Principles
- Simplicity Over Cleverness - Clear, readable code beats clever abstractions
- Composition Over Inheritance - Use interfaces and embedding
- Explicit Over Implicit - Make behavior obvious, avoid magic
- Fail Fast - Return errors early, handle them explicitly
Quick Reference
Must-Use Patterns
| Pattern |
Use Case |
| Error wrapping |
fmt.Errorf("context: %w", err) for error chains |
| Table-driven tests |
Multiple test cases in structured format |
| Functional options |
Configurable constructors with defaults |
| Interface segregation |
Small, focused interfaces (1-3 methods) |
| Context propagation |
Pass context.Context as first parameter |
Must-Avoid Anti-Patterns
| Anti-Pattern |
Alternative |
| Ignoring errors |
Always handle: if err != nil { return err } |
| Panic for expected failures |
Return error type |
| Package-level state |
Dependency injection |
| Large interfaces |
Small, composable interfaces |
| Deep package nesting |
Flat, focused packages |
init() functions |
Explicit initialization |
Detailed Guidelines
For comprehensive guidance, see:
Code Style
Naming Conventions
| Item |
Convention |
Example |
| Packages |
short, lowercase, singular |
user, http, json |
| Exported |
PascalCase |
NewServer, UserService |
| Unexported |
camelCase |
parseConfig, handleRequest |
| Interfaces |
-er suffix for single method |
Reader, Writer, Closer |
| Acronyms |
consistent case |
HTTPServer, xmlParser |
Documentation
// Package user provides user management functionality.
package user
// User represents a registered user in the system.
// It contains the user's identity and profile information.
type User struct {
ID string
Email string
Name string
}
// NewUser creates a new User with the given email.
// Returns an error if the email format is invalid.
func NewUser(email string) (*User, error) {
// ...
}
Tooling
Always run these before committing:
go fmt ./... # Format code
go vet ./... # Static analysis
go test ./... # Run tests
go mod tidy # Sync dependencies
Consider adding:
golangci-lint - Comprehensive linter
staticcheck - Advanced static analysis
References
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: go-coding-standards-33description: Use when writing, reviewing, or refactoring Go code. Provides error handling patterns, project layout, concurrency, and interface design guidelines.4---56# Go Coding Standards78This skill provides modern Go coding guidelines and best practices for this project.910## When to Apply1112Apply these standards when:13- Writing new Go code14- Reviewing or refactoring existing Go code15- Designing package APIs and interfaces16- Implementing error handling strategies1718## Core Principles19201. **Simplicity Over Cleverness** - Clear, readable code beats clever abstractions212. **Composition Over Inheritance** - Use interfaces and embedding223. **Explicit Over Implicit** - Make behavior obvious, avoid magic234. **Fail Fast** - Return errors early, handle them explicitly2425## Quick Reference2627### Must-Use Patterns2829| Pattern | Use Case |30|---------|----------|31| Error wrapping | `fmt.Errorf("context: %w", err)` for error chains |32| Table-driven tests | Multiple test cases in structured format |33| Functional options | Configurable constructors with defaults |34| Interface segregation | Small, focused interfaces (1-3 methods) |35| Context propagation | Pass `context.Context` as first parameter |3637### Must-Avoid Anti-Patterns3839| Anti-Pattern | Alternative |40|--------------|-------------|41| Ignoring errors | Always handle: `if err != nil { return err }` |42| Panic for expected failures | Return `error` type |43| Package-level state | Dependency injection |44| Large interfaces | Small, composable interfaces |45| Deep package nesting | Flat, focused packages |46| `init()` functions | Explicit initialization |4748## Detailed Guidelines4950For comprehensive guidance, see:51- [Error Handling Patterns](./error-handling.md) - Error wrapping, sentinel errors, custom types52- [Project Layout Conventions](./project-layout.md) - Standard layout, layered architecture53- [Concurrency Patterns](./concurrency.md) - Goroutines, channels, sync primitives54- [Interface Design](./interfaces-design.md) - Composition, small interfaces, mocking55- [Security Guidelines](./security.md) - Credential protection, path sanitization, sensitive data handling5657## Code Style5859### Naming Conventions6061| Item | Convention | Example |62|------|------------|---------|63| Packages | short, lowercase, singular | `user`, `http`, `json` |64| Exported | PascalCase | `NewServer`, `UserService` |65| Unexported | camelCase | `parseConfig`, `handleRequest` |66| Interfaces | -er suffix for single method | `Reader`, `Writer`, `Closer` |67| Acronyms | consistent case | `HTTPServer`, `xmlParser` |6869### Documentation7071```go72// Package user provides user management functionality.73package user7475// User represents a registered user in the system.76// It contains the user's identity and profile information.77type User struct {78 ID string79 Email string80 Name string81}8283// NewUser creates a new User with the given email.84// Returns an error if the email format is invalid.85func NewUser(email string) (*User, error) {86 // ...87}88```8990## Tooling9192Always run these before committing:9394```bash95go fmt ./... # Format code96go vet ./... # Static analysis97go test ./... # Run tests98go mod tidy # Sync dependencies99```100101Consider adding:102- `golangci-lint` - Comprehensive linter103- `staticcheck` - Advanced static analysis104105## References106107- [Effective Go](https://go.dev/doc/effective_go)108- [Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments)109- [Standard Go Project Layout](https://github.com/golang-standards/project-layout)110- [Uber Go Style Guide](https://github.com/uber-go/guide/blob/master/style.md)111112---113> Converted and distributed by [TomeVault](https://tomevault.io/claim/tacogips) — claim your Tome and manage your conversions.114<!-- tomevault:4.0:skill_md:2026-04-15 -->