golang结构定义中的backtick的用法是什么? [英] What is the usage of backtick in golang structs definition?

查看:198
本文介绍了golang结构定义中的backtick的用法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

type NetworkInterface struct {
    Gateway              string `json:"gateway"`
    IPAddress            string `json:"ip"`
    IPPrefixLen          int    `json:"ip_prefix_len"`
    MacAddress           string `json:"mac"`
    ...
}

我很困惑什么是反向内容的功能,如 json:gateway

I'm quite confused what's the function of contents in backtick, like json:"gateway".

它只是注释,就像 //这是网关

推荐答案

您可以以标签的形式为Go结构添加额外的元信息。 下面是一些用例的例子

You can add extra meta information to Go structs in the form of tags. Here are some examples of use cases.

在这种情况下, json:gateway json package Gateway 的值编码到密钥 gateway 在相应的json对象中。

In this case, the json:"gateway" is used by the json package to encode the value of Gateway into the key gateway in the corresponding json object.

示例:

Example:

n := NetworkInterface{
   Gateway : "foo"
}
json.Marshal(n)
// will output `{"gateway":"foo",...}`

这篇关于golang结构定义中的backtick的用法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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