Dictionary 如何在golang中的嵌套结构上设置范围?
我是刚到戈朗的: 这些是我定义的结构:Dictionary 如何在golang中的嵌套结构上设置范围?,dictionary,go,Dictionary,Go,我是刚到戈朗的: 这些是我定义的结构: type Name map[string]Info type Info struct { Addresses string `json:"addresses"` Host map[string]Server `json:"host"` } type Server struct { Ipaddress string
type Name map[string]Info
type Info struct {
Addresses string `json:"addresses"`
Host map[string]Server `json:"host"`
}
type Server struct {
Ipaddress string `json:"ip"`
Status string `json:"status"`
}
var result Name
解组Json后,我得到:
result = [
user1:{
192.168.98.0/26
map[
xx.user1.domain.com:{192.168.98.1 good}
xx.user1.domain.com:{192.168.98.3 good}
xx.user1.domain.com:{192.168.98.4 Bad}
]
}
user2: {
192.168.99.0/26
map[
xx.user2.domain.com:{192.168.99.1 good}
]
}
]
如何在这个Json范围内获得ipaddress,该地址对于特定用户的状态为==“good”
我正试图这样做:
for j , _ := range result["user1"].Servers {
if a := result["user1"].Servers[j]); a == "good" {
//Problem is here I am not sure how to further scan the ip and status
//do something
}
我想你想要:
for _ , i := range result {
for _, j := range i.Host {
if j.Status == "good" {
server := j.Ip
}
}
}
你试过什么?什么不起作用?如果您能向我们展示您尝试使用的代码,并在结构中穿行,这将有助于我们指导您。有
范围
或用于
。您没有其他选项。fmt.Printf()
需要一个字符串和格式,您传入了定义的结构Server
,而不是类型string
。。。请尝试fmt.Println()
。
for _ , i := range result {
for _, j := range i.Host {
if j.Status == "good" {
server := j.Ip
}
}
}