Provides a generic, type-safe atomic value wrapper with load, store, swap, and CAS operations. Reuse for concurrent Go state management.
// Source: https://github.com/zishang520/socket.io | License: MIT | Copyright 2022-2025 luoyy
// Provided via justclaw.it.com; keep this notice with the code.
package types
import (
"sync/atomic"
)
// Atomic is a type-safe atomic value container.
// The zero value is the zero value of TValue.
// An Atomic must not be copied after first use.
type Atomic[TValue any] struct {
_ noCopy
zero TValue
v atomic.Value
}
// Load atomically loads and returns the value stored in x.
// If the stored value is not of type TValue, returns the zero value of TValue.
func (s *Atomic[TValue]) Load() TValue {
if val, ok := s.v.Load().(TValue); ok {
return val
}
return s.zero
}
// Store atomically stores val into x.
func (s *Atomic[TValue]) Store(val TValue) {
s.v.Store(val)
}
// Swap atomic
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key