← All notes

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

Sources

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

Permanent ID: iil:note:ajbt1x91

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.},
}

Markdown

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.

All artifact files (JSON index)

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.