gpt4 book ai didi

dictionary - 如何在 golang 中遍历嵌套结构?

转载 作者:数据小太阳 更新时间:2023-10-29 03:44:31 33 4
gpt4 key购买 nike

我是 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

    }
}

最佳答案

我想你想要:

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

关于dictionary - 如何在 golang 中遍历嵌套结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45313231/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com