我的类型A基本上是一个简单的地图:
type A map[int32]struct{}
Run Code Online (Sandbox Code Playgroud)
现在,我想拥有这种类型的特殊价值,以便能够对其有所不同。我认为使用nil此提议会很明智(此外,通过这种方式,所有类型的所有未初始化变量都A将具有此值,这也是我想要的值):
const s A = nil
Run Code Online (Sandbox Code Playgroud)
但是我得到了
const初始化器不能为nil
当然,我可以接受这一点,并以多种不同的方式重构程序。但是我仍然想知道为什么不可能初始化const为nil?一定有架构上的原因,但我看不到。
(请注意,我更喜欢“重命名”,nil而不是直接使用它,原因仅在于该名称nil对我而言不是很直观)。
I believe that it is because you can only have constants of type boolean, rune, integer, floating-point, complex, and string.
Docs: https://golang.org/ref/spec#Constants
虽然nil是一个类型零值pointer,interface,map,slice,channel和function
文档:https://golang.org/ref/spec#The_zero_value
您可以在这里阅读更多内容https://blog.golang.org/constants
| 归档时间: |
|
| 查看次数: |
160 次 |
| 最近记录: |