在golang中为空或不需要结构字段 [英] Empty or not required struct fields in golang

查看:2260
本文介绍了在golang中为空或不需要结构字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对像Go这样的键入语言有点新,并且正在努力学习实现这些东西的最佳方式。

我有两个表示将插入到mongodb数据库中的模型的结构。一个结构(投资)有另一个结构(组)作为其领域之一。

 类型组结构{
Base
名称字符串`json:namebsonname`
}

类型投资struct {
Base
符号字符串`json:符号bson:符号绑定:必需
组合组json:groupbson:group`
字段bson.M`json:fieldsbson:fields `
}

我遇到的问题是在投资模式中,不是必需的。如果没有组,我认为它更好,因为它不会被插入数据库。在Go中处理db模型的最佳方式是什么? 使用,omitempty ,如果您需要考虑零值和null /未指定的区别,做什么GitHub API并使用指针






json bson 支持,omitempty 标签。对于json,空值是假的,0,任何零指针或接口值,以及长度为零的任何数组,切片,映射,或字符串 =http://golang.org/pkg/encoding/json/> json文档)。对于bson,,omitempty 的意思是只包含字段,如果它没有被设置为类型的零值或空片或地图,并且零值包括空字符串和零指针( bson docs )。

所以如果你确实需要一个组结构,你可以放一个 * Group ,而指针为零时不会被存储。如果投资只需要持有该组的名称,则更简单: as



bson 默认使用小写字段名称,所以您可以当它们匹配时从结构标签中省略。 json 将默认为大写字母名称,因此如果您需要小写字母,请在标记中指定小写字母名称。



,最好的情况下,也许你可以使用:

 类型投资结构{
基础
符号字符串` json:symbolbinding:required
组字符串`json:group,omitemptybson:,omitempty`
字段bson.M`json:fields`

如果您遇到类型为零(,错误等)与未指定不同,您可以执行GitHub API的功能,把指针放在你的结构中 - 基本上是 * Group 技巧的扩展。


I'm somewhat new to typed languages like Go and am trying to learn the best ways to implement things.

I have two structs that represent models that will be inserted into a mongodb database. One struct (Investment) has the other struct (Group) as one of its fields.

type Group struct {
    Base
    Name string `json:"name" bson"name"`
}

type Investment struct {
    Base
    Symbol string `json:"symbol" bson:"symbol" binding:"required"`
    Group  Group  `json:"group" bson:"group"`
    Fields bson.M `json:"fields" bson:"fields"`
}

The problem I'm having is that in the Investment model, Group is not required. If there is no group, I think its better for it to not be inserted in the db. Whats the best way to handle a db model such as this in Go?

解决方案

tl;dr: Use ,omitempty, and if you need to worry about the difference between a zero value and null/not specified, do what the GitHub API does and use a pointer.


Both json and bson support the ,omitempty tag. For json, "empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero" (json docs). For bson, ,omitempty means "Only include the field if it's not set to the zero value for the type or to empty slices or maps", and zero values include empty strings and nil pointers (bson docs).

So if you really need a Group struct, you can put a *Group in instead, and it won't be stored when the pointer is nil. If Investment only needs to hold the group's name, it's even simpler: "" as group name keeps a group key from being stored.

bson defaults to using the lowercased field name already so you can omit that from the struct tag when they match. json will default to the Capitalized name, so specify the lowercase name in a tag if you need lowercase.

So, best case, maybe you can just use:

type Investment struct {
    Base
    Symbol string `json:"symbol" binding:"required"`
    Group string  `json:"group,omitempty" bson:",omitempty"`
    Fields bson.M `json:"fields"`
}

If you ever run into fields where the zero value for the type ("", 0, false, etc.) is distinct from "not specified", you can do what the GitHub API does and put pointers in your structures--essentially an extension of the *Group trick.

这篇关于在golang中为空或不需要结构字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆