Skip to content

Instantly share code, notes, and snippets.

@koonix
Last active January 8, 2025 00:14
Show Gist options
  • Save koonix/47b796897bb476ea59cbebea43f227c5 to your computer and use it in GitHub Desktop.
Save koonix/47b796897bb476ea59cbebea43f227c5 to your computer and use it in GitHub Desktop.
Nuclear atomically stores values. The closest thing to this in the stdlib is atomic.Pointer.
import "sync"
type Nuclear[T any] struct {
mu sync.RWMutex
v T
}
func NewNuclear[T any](v T) *Nuclear[T] {
return &Nuclear{
v: v,
}
}
func (n *Nuclear[T]) Store(v T) {
n.mu.Lock()
defer n.mu.Unlock()
n.v = v
}
func (n *Nuclear[T]) Load() T {
n.mu.RLock()
defer n.mu.RUnlock()
return n.v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment