How to define multiple name tags in a struct

huangapple go评论74阅读模式
英文:

How to define multiple name tags in a struct

问题

我需要从Mongo数据库中获取一个项目,所以我定义了一个类似这样的结构体:

type Page struct {
    PageId string                 `bson:"pageId"`
    Meta   map[string]interface{} `bson:"meta"`
}

现在我还需要将其编码为JSON,但它将字段编码为大写(我得到的是PageId而不是pageId),所以我还需要为JSON定义字段标签。我尝试了类似这样的方式,但它没有起作用:

type Page struct {
    PageId string                 `bson:"pageId",json:"pageId"`
    Meta   map[string]interface{} `bson:"meta",json:"pageId"`
}

那么如何实现这个目标,在一个结构体中定义多个名称标签呢?

英文:

I need to get an item from a mongo database, so I defined a struct like this

type Page struct {
    PageId string                 `bson:"pageId"`
    Meta   map[string]interface{} `bson:"meta"`
}

Now I also need to encode it to JSON, but it encodes the fields as uppercase (i get PageId instead of pageId) so i also need to define field tags for JSON. I tried something like this but it didn't work:

type Page struct {
    PageId string                 `bson:"pageId",json:"pageId"`
    Meta   map[string]interface{} `bson:"meta",json:"pageId"`
}

So how can this be done, define multiple name tags in a struct?

答案1

得分: 360

你需要做的是使用空格而不是逗号作为标签字符串的分隔符。

type Page struct {
    PageId string                 `bson:"pageId" json:"pageId"`
    Meta   map[string]interface{} `bson:"meta" json:"meta"`
}

reflect包的文档中有这样的说明:

按照惯例,标签字符串是可选地用空格分隔的键:"值"对的串联。每个键是一个非空字符串,由非控制字符组成,除了空格(U+0020 ' ')、引号(U+0022 '"')和冒号(U+003A ':')。每个值使用U+0022 '"'字符和Go字符串字面值语法进行引用。

英文:

What you need to do is to use space instead of commas as tag string separators.

type Page struct {
    PageId string                 `bson:"pageId" json:"pageId"`
    Meta   map[string]interface{} `bson:"meta" json:"meta"`
}

It says in the documentation of the reflect package:

> By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

答案2

得分: 96

感谢接受的答案。

以下仅供像我这样懒惰的人使用。

错误的写法

type Page struct {
    PageId string                 `bson:"pageId",json:"pageId"`
    Meta   map[string]interface{} `bson:"meta",json:"pageId"`
}

正确的写法

type Page struct {
    PageId string                 `bson:"pageId" json:"pageId"`
    Meta   map[string]interface{} `bson:"meta" json:"pageId"`
}
英文:

Thanks for the accepted answer.

Below is just for the lazy people like me.

INCORRECT

type Page struct {
    PageId string                 `bson:"pageId",json:"pageId"`
    Meta   map[string]interface{} `bson:"meta",json:"pageId"`
}

CORRECT

type Page struct {
    PageId string                 `bson:"pageId" json:"pageId"`
    Meta   map[string]interface{} `bson:"meta" json:"pageId"`
}

huangapple
  • 本文由 发表于 2013年9月5日 19:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/18635671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定