去朗,结构:什么是第三个参数 [英] go lang, struct: what is the third parameter
问题描述
类型配置结构{
CommitIndex uint64`json:commitIndex`
// TODO决定我们需要在对等结构中存储什么
对等[] * Peer'json:peers`
}
。
但是,什么是json:commitIndex
谢谢。
它被称为结构标签,可以使用 从 rel =noreferrer> https://golang.org/ref/spec#Struct_types : 字段声明可以后跟一个可选的字符串文字标签,它成为相应字段声明中所有字段的属性。 标签通过反射界面变得可见并参与在结构的类型标识中,但被忽略。 一些使用像 反射 $ b
json
和 xml
使用标签更好地处理特殊情况。
type Config struct {
CommitIndex uint64 `json:"commitIndex"`
// TODO decide what we need to store in peer struct
Peers []*Peer `json:"peers"`
}
I understand first 2 colums. But, what is json:"commitIndex"
thanks.
It's called a struct tag, they can be parsed using the reflect
package at runtime.
From 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.
Some packages that use reflection like json
and xml
use tags to handle special cases better.
这篇关于去朗,结构:什么是第三个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!