go - Go 结构字段的第三个参数是什么?

标签 go

type Config struct {
    CommitIndex uint64 `json:"commitIndex"`
    // TODO decide what we need to store in peer struct
    Peers []*Peer `json:"peers"`
}

我知道前两列是什么,但是 json:"commitIndex" 是什么?

最佳答案

称为结构标签,可以使用reflect 解析。在运行时打包。

来自 https://golang.org/ref/spec#Struct_types :

A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration.

The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.

一些使用反射的包如 jsonxml使用标签更好地处理特殊情况。

关于go - Go 结构字段的第三个参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25497375/

相关文章:

winapi - syscall.Syscall 和 WinAPI 遇到问题

xml - 如何在 Go 中解码包含脏 HTML 的 XML

go - `size *= b - a` 是什么意思?

json - 如何构建 JSON 以便我可以使用 golang 接收 int64 和字符串?

go - 更新 yaml 文件中的属性

json - 从键值对中提取 "filtering"JSON 对象的最有效方法是什么?

具有自定义签名的 Golang 错误组超过函数

go - 如何在 GoLand 中运行整个项目?

go - 为什么http.HandleFunc对一个请求执行两次?

.htaccess - Heroku htaccess 基本身份验证上的 Golang 应用程序