Menu

Post image 1
Post image 2
Post image 3
Post image 4
Post image 5
1 / 5
0

sync.OnceFunc, OnceValue, OnceValues: When Each Beats sync.Once

DEV Community·Gabriel Anhaia·25 days ago
#LC3Cy0Cn
#concurrency#go#stdlib#once#sync#func
Reading 0:00
15s threshold

Book: Complete Guide to Go Programming Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub Open any Go codebase past a year old and you will hit this pattern: var ( cfgOnce sync . Once cfg * Config cfgErr error ) func GetConfig () ( * Config , error ) { cfgOnce . Do ( func () { cfg , cfgErr = loadConfig () }) return cfg , cfgErr } Enter fullscreen mode Exit fullscreen mode Three package-level variables. A closure that mutates two of them by name. A function that reads them after Do returns. It works. It has worked since Go 1.0. It also leaks state into package scope, makes the call site harder to reason about, and gives you nothing to inject into a struct field if you want a per-instance memoized initializer.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More