go-performancelisted
Install: claude install-skill dwana1/golang-skills
# Go Performance Patterns
> **Source**: Uber Go Style Guide
Performance-specific guidelines apply only to the **hot path**. Don't prematurely optimize—focus these patterns where they matter most.
---
## Prefer strconv over fmt
When converting primitives to/from strings, `strconv` is faster than `fmt`.
> **Source**: Uber Go Style Guide
**Bad:**
```go
for i := 0; i < b.N; i++ {
s := fmt.Sprint(rand.Int())
}
```
**Good:**
```go
for i := 0; i < b.N; i++ {
s := strconv.Itoa(rand.Int())
}
```
**Benchmark comparison:**
| Approach | Speed | Allocations |
|----------|-------|-------------|
| `fmt.Sprint` | 143 ns/op | 2 allocs/op |
| `strconv.Itoa` | 64.2 ns/op | 1 allocs/op |
---
## Avoid Repeated String-to-Byte Conversions
Do not create byte slices from a fixed string repeatedly. Instead, perform the conversion once and capture the result.
> **Source**: Uber Go Style Guide
**Bad:**
```go
for i := 0; i < b.N; i++ {
w.Write([]byte("Hello world"))
}
```
**Good:**
```go
data := []byte("Hello world")
for i := 0; i < b.N; i++ {
w.Write(data)
}
```
**Benchmark comparison:**
| Approach | Speed |
|----------|-------|
| Repeated conversion | 22.2 ns/op |
| Single conversion | 3.25 ns/op |
The good version is **~7x faster** because it avoids allocating a new byte slice on each iteration.
---
## Prefer Specifying Container Capacity
Specify container capacity where possible to allocate memory up front. This minimizes subsequent allocations from copying a