---
title: Go Best Practices
type: agent/instruction
tags: [go, golang]
---

{{h1}}Go

{{h2}}Error Handling
- Always check errors
- Return errors, don't panic
- Wrap with `fmt.Errorf` and `%w`
- Use `errors.Is` and `errors.As`

{{h2}}Interfaces
- Small and focused
- Define where used, not where implemented
- Accept interfaces, return structs

{{h2}}Concurrency
- Goroutines for concurrent ops
- Channels for communication
- Close channels when done sending
- Use `context.Context` for cancellation

{{h2}}Code
- Short names for short-lived variables
- Use `defer` for cleanup
- Table-driven tests
- Don't stutter names: `http.Server` not `http.HTTPServer`

{{h2}}Avoid
- Don't panic in libraries
- Don't ignore errors
- Don't use global variables
- Don't use `init()` for complex setup

