Last active
October 5, 2024 17:33
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nuke | |
import "sync" | |
type Nuclear[T any] struct { | |
mu sync.RWMutex | |
v T | |
} | |
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