如何在golang中覆盖嵌套结构?

时间:2017-07-25 20:48:42

标签: dictionary go

我是Golang的新手: 这些是我定义的结构:

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来获得状态为==" good"的ipaddress。对于特定用户?

我正在尝试这样做:

  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

}

    }
}

1 个答案:

答案 0 :(得分:1)

我想你想要:

for _ , i := range result {
    for  _, j := range i.Host {
        if j.Status == "good" {
            server := j.Ip
        }
    }
}