英文:
when to use atomic.Pointer[T] versus atomic.Value
问题
go1.19引入了atomic.Pointer,我注意到一些源代码已经从atomic.Value转移到了atomic.Pointer。(例如:426074: sync: switch Map to use atomic.Pointer,422174: encoding/gob: change typeInfo.encoder type to atomic.Pointer[T])
所以我的问题是:
- 如果我的代码考虑使用泛型,是否可以将所有的atomic.Value转换为atomic.Pointer,在什么情况下应该更多地使用atomic.Value?
- atomic.Value的存在只是为了兼容性的原因吗?是否应该逐渐废弃atomic.Value?
英文:
go1.19 introduce atomic.Pointer, and I noticed Some source code has moved from atomic.Value to atomic.Pointer. (ex: 426074: sync: switch Map to use atomic.Pointer, 422174: encoding/gob: change typeInfo.encoder type to atomic.Pointer[T])
So my question are:
- If my code considers using generics, can all atomic.Value's be converted to atomic.Pointer's and what are the cases where atomic.Value's should be used more?
- Is the existence of atomic.Value only for compatibility reasons, should atomic.Value be gradually deprecated?
答案1
得分: 2
更喜欢使用更具体的原子类型而不是atomic.Value
。就像在处理bool
值时更喜欢使用atomic.Bool
而不是atomic.Value
一样,在处理指针时更喜欢使用atomic.Pointer
而不是atomic.Value
。
> 如果我的代码考虑使用泛型,是否可以将所有的atomic.Value
转换为atomic.Pointer
?在哪些情况下应该更多地使用atomic.Value
?
当值是指针时,可以将atomic.Value
转换为atomic.Pointer
。
> atomic.Value
的存在只是为了兼容性的原因吗?是否应该逐渐弃用atomic.Value
?
比atomic.Value
更具体的原子类型并不能涵盖所有可能的类型。因此仍然需要atomic.Value
。
英文:
Prefer more specific atomic types over atomic.Value
. Just as atomic.Bool
is preferred over atomic.Value
when working with bool
values, atomic.Pointer
is preferred over atomic.Value
when working with pointers.
> If my code considers using generics, can all atomic.Value's be converted to atomic.Pointer's and what are the cases where atomic.Value's should be used more?
Convert from atomic.Value
to atomic.Pointer
when the value is a pointer.
> Is the existence of atomic.Value only for compatibility reasons, should atomic.Value be gradually deprecated?
The atomic types that are more specific than atomic.Value
do not cover all possible types. There's still a need atomic.Value
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论