去朗,结构:什么是第三个参数 [英] go lang, struct: what is the third parameter

查看:86
本文介绍了去朗,结构:什么是第三个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 类型配置结构{
CommitIndex uint64`json:commitIndex`
// TODO决定我们需要在对等结构中存储什么
对等[] * Peer'json:peers`
}


但是,什么是json:commitIndex

谢谢。

解决方案 div>

它被称为结构标签,可以使用 反射 $ b

rel =noreferrer> https://golang.org/ref/spec#Struct_types


字段声明可以后跟一个可选的字符串文字标签,它成为相应字段声明中所有字段的属性。



标签通过反射界面变得可见并参与在结构的类型标识中,但被忽略。

一些使用像 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆