Why does a Go service suddenly burn CPU on simple HTTP calls, how does the new JSON parser avoid if branches, and why does a CLI tool need a fallback version variable? Here are five under-the-hood Go breakdowns with working code, benchmarks, and real-world limits.
How encoding/json/v2 works internally
In Go 1.27.3, encoding/json/v2 is enabled by default, and the legacy encoding/json package wraps it. A layered walkthrough shows three tiers: internal/jsonwire (raw bytes), encoding/json/jsontext (tokens and grammar without Go types), and encoding/json/v2, which binds JSON to structs via a cached arshaler (a pair of marshal and unmarshal functions). Options flow through jsonopts and jsonflags, letting you pass jsontext settings directly to json.Marshal and json.Unmarshal. Boolean flags sit in 64-bit Presence and Values bitmasks (jsonflags.Flags), and Set() updates them branchlessly by multiplying the mask by 0 or 1.
import (
json "encoding/json/v2"
"encoding/json/jsontext"
)
// Syntax-layer jsontext options pass directly into v2
var u User
err := json.Unmarshal(data, &u, jsontext.AllowDuplicateNames(true))
if err != nil {
panic(err)
}
out, err := json.Marshal(u, jsontext.Multiline(true))
Existing v1 code continues to work unchanged, while places needing strict validation or new options only have to switch their import to encoding/json/v2.
The hidden penalty of Go's default HTTP client
http.DefaultClient caps http.DefaultMaxIdleConnsPerHost at 2. Over HTTP/2 that barely shows, but in HTTP/1.1 every concurrent request needs its own TCP socket.
In a Go 1.26.1 benchmark (4-core Linux VM, 1 KiB responses), 64 workers on http.DefaultClient hit 2,721 req/s versus 22,922 req/s with a larger pool and half the CPU usage. perf showed 85% of CPU (3.35 cores vs. 0.28 Go cores) burned inside Linux's __inet_hash_connect. At ~1,600 reconnects/s and a 60-second TIME_WAIT, the client hit net.ipv4.tcp_max_tw_buckets = 16384: all ~14,000 even ports out of 28,232 (32768–60999) filled up, forcing connect() to scan them fruitlessly over 20 million times/s before trying odd ports.
// Clone the default transport to preserve timeouts and proxy settings
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.MaxIdleConnsPerHost = workers
client := &http.Client{Transport: tr}
Three lines eliminate kernel port scanning and speed up HTTP/1.1 by roughly 8x; checking your transport pool is smart even on HTTP/2.
HTTP/3 from scratch in Go: from browser to custom server
HTTP/3 runs over UDP/QUIC without TCP ALPN. A from-scratch Go HTTP/3 guide walks through two discovery mechanisms: the alt-svc: h3=":443"; ma=86400 header (starting on the second request) and the DNS HTTPS record (RFC 9460, alpn="h3,h2"), which switches to UDP immediately.
Locally, curl --http3-only -k accepts a self-signed certificate, whereas Chrome rejects it over QUIC—you need mkcert and --origin-to-force-quic-on=localhost:8443. Under RFC 9114 (6.2.1), both sides open a unidirectional QUIC stream 0x00 (control stream) carrying a SETTINGS (0x04) frame; closing it terminates the connection with H3_CLOSED_CRITICAL_STREAM.
// Thin wrapper over quic-go for swapping in a custom UDP transport later
type Conn interface {
AcceptStream(ctx context.Context) (Stream, error)
OpenStreamSync(ctx context.Context) (Stream, error)
AcceptUniStream(ctx context.Context) (ReceiveStream, error)
OpenUniStreamSync(ctx context.Context) (SendStream, error)
}
An educational server behind this interface talks to curl, quic-go, and Chrome; it isn't production-ready (SettingsQPACKMaxTableCapacity = 0), yet it strips away the protocol's mystery.
Probabilistic data structures in Go: binary fuse filters, UltraLogLog, and MinHash
The nalgeon/unsure package and its accompanying walkthrough bundle four compact algorithms in Go: binary fuse filter, UltraLogLog, log-linear histogram, and MinHash.
A binary fuse filter is static, yet for 1 million items (0.39% FP, uint8) it uses 1.13 MB (9 bits/item) versus 1.51 MB (12 bits) for Bloom—36% smaller. Contains XORs three cells in 7.0 ns (vs. 9.3 ns for FastFilter/xorfilter and 10.2 ns for Bloom), though construction is up to 2.5x slower. UltraLogLog (Otmar Ertl, 2024) is 43% smaller than HyperLogLog (8 KB at 0.86% error vs. 16 KB at 0.81%), though slower on inserts. Log-linear histograms and MinHash round out the set.
items := [][]byte{[]byte("alice"), []byte("bob")}
// 8-bit filter: 0.39% false-positive rate (~9 bits per item)
filter := fuse.New[uint8](slices.Values(items), len(items))
ok := filter.Contains([]byte("alice")) // true
For immutable key sets (revoked tokens, indexes), a binary fuse filter saves a third of RAM and speeds up lookups without Cgo.
Letting package maintainers override a Go binary's version
CLI tools often read their version from VCS via debug.ReadBuildInfo(). However, Nix and Linux distro builders compile source tarballs without .git, leaving --version blank or printing (devel)—as happened in oapi-codegen.
A Git-free versioning pattern compatible with GoReleaser declares var version = "dev" in main for go build -ldflags='-X main.version=1.2.3'. At runtime, the code checks main.version first and falls back to debug.ReadBuildInfo() (if info.Main.Version is non-empty and not "(devel)") or versioninfo.
var version = "dev"
// Prefer -ldflags version first, then fall back to BuildInfo
func ToolVersion(mainVersion string) string {
if mainVersion != "dev" && mainVersion != "" {
return mainVersion
}
info, ok := debug.ReadBuildInfo()
if ok && info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
return ""
}
Standard go install still picks up tags from BuildInfo, while Nix and Linux packagers get a clean way to set versions without patching.


