2 回答
TA贡献2012条经验 获得超12个赞
MongoDB 驱动程序仅使用bson
标签。json
标签仅用于encoding/json
包(或其他处理 JSON 编组/解组的第 3 方包)。
您不需要指定和使用bson
标签,在这种情况下,驱动程序在编码结构值时通常只使用小写的字段名称。bson
但是,当您需要不同的名称时,标签是必需的。
bson
即使您想使用小写的字段名称,指定标签也是一个好习惯,因为有时您可能需要重命名结构字段,这会导致麻烦和不一致。如果您指定bson
标签,那么将来是否重命名字段都没有关系,它们仍将被编组到相同的属性中,并且解组它们将继续起作用。
TA贡献1815条经验 获得超10个赞
bson 标签也可以包含改变默认封送行为的标志:
OmitEmpty Only include the field if it's not set to the zero value for the type or to
empty slices or maps.
MinSize Marshal an integer of a type larger than 32 bits value as an int32, if that's
feasible while preserving the numeric value.
Truncate When unmarshaling a BSON double, it is permitted to lose precision to fit within
a float32.
Inline Inline the field, which must be a struct or a map, causing all of its fields
or keys to be processed as if they were part of the outer struct. For maps,
keys must not conflict with the bson keys of other struct fields.
Skip This struct field should be skipped. This is usually denoted by parsing a "-"
for the name.
- 2 回答
- 0 关注
- 215 浏览
添加回答
举报