go
50 lines · 8 steps
Pooling buffers for logfmt encoding in Go
A sync.Pool recycles byte buffers so each log record is built without fresh allocations.
Explained by
highlit
1package logfmt
2
3import (
4 "bytes"
5 "io"
6 "sync"
7)
8
9var bufferPool = sync.Pool{
10 New: func() any {
11 return new(bytes.Buffer)
12 },
13}
14
15func getBuffer() *bytes.Buffer {
16 return bufferPool.Get().(*bytes.Buffer)
17}
18
19func putBuffer(buf *bytes.Buffer) {
20 if buf.Cap() > 64<<10 {
21 return
22 }
23 buf.Reset()
24 bufferPool.Put(buf)
25}
26
27func WriteRecord(w io.Writer, fields map[string]string) (int, error) {
28 buf := getBuffer()
29 defer putBuffer(buf)
30
31 first := true
32 for key, value := range fields {
33 if !first {
34 buf.WriteByte(' ')
35 }
36 first = false
37 buf.WriteString(key)
38 buf.WriteByte('=')
39 if bytes.ContainsAny([]byte(value), " \"") {
40 buf.WriteByte('"')
41 buf.WriteString(value)
42 buf.WriteByte('"')
43 } else {
44 buf.WriteString(value)
45 }
46 }
47 buf.WriteByte('\n')
48
49 return w.Write(buf.Bytes())
50}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A sync.Pool amortizes allocation cost by reusing temporary objects across calls.
- 2Capping the size of returned objects prevents the pool from hoarding oversized buffers.
- 3Always reset pooled objects before returning them so stale data never leaks into the next user.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
go
package model import ( "encoding/json"
Custom JSON marshaling in Go
json
serialization
interfaces
Intermediate
5 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
go
func (h *TransactionHandler) ExportCSV(c *gin.Context) { ctx := c.Request.Context() filters := parseTransactionFilters(c)
Streaming a CSV export in Gin
streaming
csv-export
database-cursor
Intermediate
8 steps
go
package store import ( "database/sql"
Wrapping and inspecting errors in Go
error-handling
error-wrapping
sentinel-errors
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/pooling-buffers-for-logfmt-encoding-in-go-explained-go-c156/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.