Go
02 Sep 2026
4 min read
Lazy Initialization in Go with sync.OnceValue and sync.OnceValues
Lazy initialization is useful when a value is expensive to build and may never be needed. The difficulty is making that initialization safe when several goroutines request the value at the same time. Go has long provided sync.Once. Since Go 1.21, the sync package also includes OnceValue and OnceValues, helpers that return functions which compute results once and reuse them for later calls. The manual sync.Once pattern A classic implementation looks like this: