为什么结构字段的格式字符串总是小写?

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

Why is the format string of struct field always lower case

问题

在使用JSON编码/解码结构体时,几乎所有的代码都使用相同的字段名,但首字母小写,这是为什么呢?

由于名称是相同的,而且JSON可以处理任何大小写,为什么要添加这个重复的东西呢:

Name string `json:"name"`

为什么不只使用 Name string?在其他情况下,如果名称与Go字段名不同,添加格式字符串是有意义的:

Name string `json:"MyName"`
英文:

When encoding/decoding structs with json, almost all of the code out there use the same field name, but with the initial letter in lower case, why is this?

Since the names are the same, and json certainly can work with any case, why add this duplicate thing:

Name string `json:"name"`

Why not just use Name string? It other case, adding the format string makes sense if the name is different than the go field name:

Name string `json:"MyName"`

答案1

得分: 5

encoding/json文档中提到:

每个结构字段的编码可以通过存储在结构字段标签下的“json”键的格式字符串进行自定义。格式字符串给出字段的名称,可能后跟逗号分隔的选项列表。名称可以为空,以便在不覆盖默认字段名称的情况下指定选项。

应用程序在标签中指定小写名称以在JSON中生成小写名称。

这个结构体

type Example struct {
  Name1 string
  Name2 string `json:"name1"`
}

编码为:

{
  "Name1": "1",
  "name1": "2"
}

playground示例

JSON只要求字段名是有效的字符串。在JSON中不要求使用小写名称。尽管如此,在JSON中以小写字母开头的字段名是非常常见的做法。

英文:

The encoding/json documentation says:

> The encoding of each struct field can be customized by the format string stored under the "json" key in the struct field's tag. The format string gives the name of the field, possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name.

Applications specify a lowercase name in the tag to produce a lowercase name in the JSON.

This struct

type Example struct {
  Name1 string
  Name2 string `json:"name1"`
}

encodes as:

{
  "Name1": "1",
  "name1": "2"
}

playground example

JSON only requires that field names be valid strings. Lowercase names are not required in JSON. That said, it is very common practice to start field names with a lowercase letter in JSON.

答案2

得分: 1

Name string json:"name" db:"SomeName"

请注意,字符串 json:"name" db:"Name" 用于调整序列化/反序列化,可以用于 JSON 或数据库。

对于命名,取决于输出。如果数据库字段是 SomeName,那么你必须定义数据库 SomeName

> 所以我的问题是为什么几乎所有的应用程序都希望使用小写?

如果你遇到使用小写输出 JSON 的源代码,显然是为了保持输出的一致性。

如果变量使用小写,也会产生不同的效果,因为小写作为 private 变量,而大写作为 public 变量,因此可以通过包访问。

英文:
Name string `json:"name" db:"SomeName"`

Keep in mind, string json:"name" db:"Name" used to adjust de/serialization, can be in json or database.

for naming it depends on output. if database field is SomeName so you must define db SomeName.

> So my questions goes to why almost all the applications want to use the lowercase?

if you encounter source code which using ouput json using only lowercase, this obviously to keep consistency output.

if lower case on variable give different effect too, for lower case act as private variable and upper case act as public variable so can be accessed through package.

答案3

得分: 1

当使用JSON编码/解码结构体时,几乎所有的代码都使用相同的字段名,但是首字母小写,这是为什么呢?

这是因为JavaScript传统上/首选使用驼峰命名法来命名变量和函数,因此自然而然地,JSON(起源于JavaScript世界)也遵循了这种方式。

当然,这并不是一种强制性的标准,存在许多竞争的标准。但是,由于问题是为什么这种情况很常见,这似乎是最有可能的答案。

当然,你可以自由选择任何你想要的大小写系统来命名JSON键名,并且你肯定会在实际软件中找到使用任何大小写系统(包括没有系统)的示例。

英文:

> When encoding/decoding structs with json, almost all of the code out there use the same field name, but with the initial letter in lower case, why is this?

Because JavaScript traditionally/preferentially uses camelCase for variable and function names, so naturally JSON (originating in the JavaScript world) followed suit.

Of course this is not an enforce standard, and there are many competing standards. But since the question is why is this common, this seems the most likely answer.

You are, of course, free to use any casing system you want for JSON key names, and you most certainly will find examples of any casing system (including lack of system) in use in real software.

huangapple
  • 本文由 发表于 2017年4月17日 10:12:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/43444248.html
匿名

发表评论

匿名网友

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

确定