Canonical guidance

Use when

Avoid

Preferred pattern

func BenchmarkDecode(b *testing.B) {
	payload := []byte(`{"name":"gopher"}`)

	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var req Request
		if err := json.Unmarshal(payload, &req); err != nil {
			b.Fatal(err)
		}
	}
}

Anti-pattern

Explanation: This anti-pattern is common because GC knobs look like easy wins, but without workload data they usually mask the real allocation problem.

Why

Sources