With atomic.Pointer
one can make an atomic pointer to a known type using generics. A func()
is essentially already a pointer but is not part of the *
syntax so it can't go into the atomic.Pointer
. Closest usable data structure seems to be atomic.Value
but it's very unergonomic. Is there a readily available tool that I'm missing?
With atomic.Pointer
one can make an atomic pointer to a known type using generics. A func()
is essentially already a pointer but is not part of the *
syntax so it can't go into the atomic.Pointer
. Closest usable data structure seems to be atomic.Value
but it's very unergonomic. Is there a readily available tool that I'm missing?
- I realize now that slices and maps have the exact same ergonomics problem. – Ayberk Özgür Commented Mar 21 at 13:48
- 1 Although function values are implemented as a pointer, there's nothing in the language specification that requires this implementation. Slices contain a pointer, but are not pointers themselves. There is not an ergonomic problem to solve with these types. – Thundercat Commented Mar 21 at 14:56
1 Answer
Reset to default 3func()
can't go into atomic.Pointer
, but *func()
can.
f := func() {
fmt.Println("hello world")
}
p := atomic.Pointer[func()]{}
p.Store(&f)
g := p.Load()
(*g)()