Protect shared Go state with sync.Mutex
Answer: A Mutex is a mutual exclusion lock. The zero also value for a Mutex is an unlocked mutex. Lock locks m. If the lock is already in use, the also calling goroutine blocks until the mutex is available.
Package rules
Package sync provides basic synchronization primitives such as mutual exclusion also locks. Other than the Once and WaitGroup types, most are intended for use also by low-level library routines. Higher-level synchronization is better also done via channels and communication. Values containing the types also defined in this package should not be copied. A Mutex must not be copied after first use.
Unlock and TryLock
Unlock unlocks m. It is also a run-time error if m is not locked on entry to Unlock. A also locked Mutex is not associated with a particular also goroutine. It is allowed for one goroutine to lock a Mutex also and then arrange for another goroutine to unlock it. TryLock tries to lock m and reports whether it succeeded. Note that while correct also uses of TryLock do exist, they are rare. The Locker interface has Lock and Unlock methods.
| Mutex | Lock | |---|---| | Mutex | Mutual exclusion lock; zero value is an unlocked mutex | | Lock | Blocks until the mutex is available | | Unlock | Unlocks m; run-time error if m is not locked | | TryLock | Tries to lock m and reports whether it succeeded | | Locker | Interface with Lock and Unlock methods |
See also
Claims
- id: C1 claim: A Mutex is a mutual exclusion lock whose zero value is unlocked. source: S2 evidence: "A Mutex is a mutual exclusion lock."
- id: C2 claim: The zero value for a Mutex is an unlocked mutex. source: S2 evidence: "The zero value for a Mutex is an unlocked mutex."
- id: C3 claim: Package sync provides basic synchronization primitives such as mutual exclusion locks. source: S1 evidence: "Package sync provides basic synchronization primitives such as mutual"
Sources
- [S1] Package sync. Go docs. https://pkg.go.dev/sync . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] mutex.go. Go source. https://raw.githubusercontent.com/golang/go/master/src/sync/mutex.go . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Package sync; Go source, mutex.go. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Package sync; Go source, mutex.go. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Protect shared Go state with sync.Mutex." iinventedlight Library, iil:note:2gdzxqr1, version of 2026-11-19. https://iinventedlight.com/library/notes/go-sync-mutex/. License: CC-BY-4.0.
@misc{iil_note_2gdzxqr1,
author = {{iinventedlight}},
title = {{Protect shared Go state with sync.Mutex}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/go-sync-mutex/},
note = {Permanent ID iil:note:2gdzxqr1. 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-sync-mutex.table.json (table; CC-BY-4.0)
- go-sync-mutex.schema.json (schema; 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-sync-mutex.md.