sync.Once: run an action exactly once
Answer: Once is an object that will perform exactly one action. Call Do(f) to run that action. Future calls of Do return without calling f. A Once must not be copied after first use.
The Once contract
The Once type is an object whose job is to perform one action, and only one.
After first use, copying a Once is not allowed.
Call Do(f) as many times as you want; only that first call will invoke f. Only the first call will invoke f. A new instance of Once is required for each function to execute.
Do and initialization
Package docs repeat that Once will perform exactly one action.
Do is intended for initialization work that has to run exactly once.
Do does not return until f returns. If f causes Do to be called, it will deadlock.
See also
Claims
- id: C1 claim: Once is an object that will perform exactly one action. source: S1 evidence: "will perform exactly one action"
- id: C2 claim: A Once must not be copied after first use. source: S1 evidence: "must not be copied after first use"
- id: C3 claim: Do is intended for initialization that must be run exactly once. source: S2 evidence: "initialization that must be run exactly once"
- id: C4 claim: If f causes Do to be called, it will deadlock. source: S2 evidence: "if f causes Do to be called, it will deadlock"
Sources
- [S1] sync/once.go. Go project. https://go.dev/src/sync/once.go . License: BSD-3-Clause. Accessed 2026-11-23.
- [S2] sync package. Go project. https://pkg.go.dev/sync . License: CC-BY-4.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go project, sync/once.go; Go project, sync package. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go project, sync/once.go; Go project, sync package. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "sync.Once: run an action exactly once." iinventedlight Library, iil:note:ajbt1x91, version of 2026-11-23. https://iinventedlight.com/library/notes/go-once/. License: CC-BY-4.0.
@misc{iil_note_ajbt1x91,
author = {{iinventedlight}},
title = {{sync.Once: run an action exactly once}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/go-once/},
note = {Permanent ID iil:note:ajbt1x91. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-once.schema.json (schema; CC-BY-4.0)
- go-once.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-once.md.