2017-07-25 4 views
-4

私はGolangに新しいです: これらは私の定義された構造体です: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} 
     ] 
    } 
] 

のステータスを持っているIPアドレスを取得するには、このJSONを超える範囲にどのように=特定のユーザーにとっては「良い」ですか?

私はこの方法を実行しようとしてい

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 

}

} 
} 
+5

あなたは何を試してみましたか?何がうまくいかないの?あなたが私たちがあなたを導くのに役立つ構造を試して歩くために使用しているコードを私たちに示すことができるならば。 – Guildencrantz

+0

'range'か' for'があります。他のオプションはありません。 –

+0

'fmt.Printf()'は文字列と書式設定を期待していますが、 'string'型の代わりに、定義されたstruct' Server'を渡しました。代わりに 'fmt.Println()'を試してみてください。 – reticentroot

答えて

1

私はあなたがしたいと思う:

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