type NetworkInterface struct {
Gateway string `json:"gateway"`
IPAddress string `json:"ip"`
IPPrefixLen int `json:"ip_prefix_len"`
MacAddress string `json:"mac"`
...
}
Run Code Online (Sandbox Code Playgroud)
我很困惑什么是反引号内容的功能,比如json:"gateway".
这只是评论//this is the gateway吗?
type Config struct {
CommitIndex uint64 `json:"commitIndex"`
// TODO decide what we need to store in peer struct
Peers []*Peer `json:"peers"`
}
Run Code Online (Sandbox Code Playgroud)
我理解前两列是什么,但是什么是json:"commitIndex"?
我一直试图找出如何使用mgo(Go的MongoDB驱动程序),我遇到了这个结构声明:
type Something struct {
Id bson.ObjectId "_id,omitempty"
Name string
}
Run Code Online (Sandbox Code Playgroud)
我不太了解第一个元素(Id)的语法.我知道它被声明为类型bson.ObjectId,但字符串文字在那里做什么?
我的问题不是关于mgo驱动程序的功能,
而是关于这种奇怪的<name> <type> <string_literal>语法.
我在Go规格上找不到任何东西,我也不知道如何谷歌这个.
看看在这里找到的这个片段
进口 (
“编码/xml”
“FMMT”
“操作系统”
)
函数主() {
类型地址结构体{
城市、州字符串
}
类型 Person 结构 {
XMLName xml.Name `xml:"person"`
Id int `xml:"id,attr"`
名字字符串 `xml:"name>first"`
姓氏字符串 `xml:"name>last"`
年龄 int `xml:"年龄"`
高度 float32 `xml:"height,omitempty"`
已婚布尔
地址
注释字符串 `xml:",comment"`
}
v := &Person{ID:13,名字:“John”,姓氏:“Doe”,年龄:42}
v.Comment =“需要更多详细信息。”
v.Address = 地址{"安加罗阿", "复活节岛"}
enc := xml.NewEncoder(os.Stdout)
enc.缩进(“”,“”)
if err := enc.Encode(v); 错误!=零{
fmt.Printf("错误: %v\n", err)
}
}
我可以理解struct Person,它有一个名为 var Id,它是类型的int,但是这些东西呢
xml:"person" 在 int 之后?这是什么意思?谢谢。 在这个结构定义中:
type API struct {
Message string "json:message"
}
Run Code Online (Sandbox Code Playgroud)
字符串“json:message”的含义是什么以及如何访问它(如果可以访问)。先感谢您。