uber-go / atomic

Wrapper types for sync/atomic which enforce atomic access
https://go.uber.org/atomic
MIT License
1.33k stars 100 forks source link

support json encode decode for Pointer #178

Closed trim21 closed 4 weeks ago

trim21 commented 1 month ago

many atomic value support MarshalJSON and UnmarshalJSON, any plan for Pointer[T]?

JacobOaks commented 1 month ago

Hey, to better understand - how are you running into this issue/what's your use case?

trim21 commented 1 month ago

Hey, to better understand - how are you running into this issue/what's your use case?

i have a config file will be used in multiple goroutine, I don't want to write a duplicate thread safe structure

type Config struct {
    Opt atomic.Pointer[string] `json:"opt"`
}

config.Opt may be write and read concurrently, and initial parsed from config, maybe nil.

Currently I need to do it this way:

type Config struct {
    Opt *string `json:"opt"`
}

type safeConfig struct {
    Opt atomic.Pointer[string]
}