Struct for JSON objects in GO

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

Struct for JSON objects in GO

问题

我正在学习GO语言,当定义用于处理JSON的结构体时,像下面这样。

type List struct {
    ID   string `datastore:"-"`
    Name string
}

我注意到在 ` 符号之间有这段文本。我还没有找到解释它的含义的说明。

即使没有这些文本,事情似乎也能正常工作。

英文:

I'm learning GO and when defining structs for working with JSON like below.

type List struct {
    ID   string `datastore:"-"`
    Name string
}

I see that there is this text in between ` sign. I have not been able to find an explanation what that signifies.

Things seem to work even without those.

答案1

得分: 3

这些是用于将Go结构体编组为JSON的结构标签。在JSON中,与Go不同,字段是小写字符串。因此,大多数情况下会这样使用:

type List struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

在JSON中:

{
  "id": "some id",
  "name": "some name"
}

在这里查看帖子here

英文:

They are struct tags used in Marshal'ing Go struct into JSON. In JSON, unlike Go, fields are in lowercase strings. Therefore, most use cases would be

type List struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

In JSON

{
  "id": "some id",
  "name": "some name"
}

See post here

huangapple
  • 本文由 发表于 2015年8月16日 07:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/32030407.html
匿名

发表评论

匿名网友

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

确定