GO

Go Performance

Go performance patterns

Details

Language / Topic
goGo
Category
Performance

Rules

balanced
- Preallocate slices and maps when size is known: `make([]T, 0, n)`, `make(map[K]V, n)`.
- Use `strings.Builder` for string concatenation in loops instead of `+` operator.
- Use `pprof` for CPU and memory profiling — run with `go tool pprof` and analyze flame graphs before optimizing.
- Use `sync.Pool` for frequently allocated temporary objects to reduce GC pressure.
- Prefer value receivers for small structs (under ~64 bytes) to avoid heap allocation.
- Use `sync.Once` for expensive one-time initialization instead of init locks.
- Use buffered channels when producer/consumer rates differ — unbuffered channels serialize execution.