go结构中有一个空结构作为字段的目的是什么?
例如:go结构中有一个空结构作为字段的目的是什么?,go,Go,例如: rawVote struct { _struct struct{} `codec:",omitempty,omitemptyarray"` Sender basics.Address `codec:"snd"` Round basics.Round `codec:"rnd"` Period period `codec:"per"`
rawVote struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Sender basics.Address `codec:"snd"`
Round basics.Round `codec:"rnd"`
Period period `codec:"per"`
Step step `codec:"step"`
Proposal proposalValue `codec:"prop"`
}
查看库实现(这里可能使用了它的一个分支),一个名为\u struct
的私有字段用于为结构的每个字段定义标记“设置”:
键入MyStruct struct{
_struct bool`codec:,ommitempty`//为每个字段设置ommitempty
字段1字符串`编解码器:“-”`//跳过此字段
Field2 int`codec:“myName”`//在编码流中使用键“myName”
Field3 int32`编解码器:“,省略空”`//使用键“Field3”。如果为空,则省略。
Field4 bool`codec:“f4,省略空”`//使用键“f4”。如果为空,则省略。
io.Reader//使用“Reader”键。
MyStruct`codec:“my1”`//使用键“my1”。
MyStruct//inline it
...
}
之所以使用struct{}
type而不是bool
,是因为它实际上意味着“无数据”,在这里看起来更合适
来源:相关:@ttrasn我已经阅读了您提供的问题和答案,但我无法找出此特定示例中存在空结构的原因。我同意VisioN,我想了解为什么会有_structstruct{}它的用例是什么。我已经查看了该项目的源代码,但找不到示例用例。