am getting the following error , when am trying add map to interface :
invalid operation: msgs["Application"]["instance-id"] (type interface {} does not support indexing)
application :
resultChannel := make(chan map[string]interface{})
clients := make(map[string][]map[string]interface{})
go func(clients map[string][]map[string]interface{}){
for {
msgs := <- resultChannel
url := "http://xxxxxx/results/"+msgs["instance-id"].(string)
res,err := http.Get(url)
if err != nil{
panic(err.Error())
}
body,err := ioutil.ReadAll(res.Body)
if err != nil{
panic(err.Error())
}
s, err := getResults([]byte(body))
for i := range s {
clientResults := map[string]interface{}{
s[i].Check.Name:s[i].Status,
}
clients[msgs["Application"]["instance-id"]["checks"]]=append(clients[msgs["Application"]["instance-id"]["checks"]],clientResults)
}
}
}(clients)
for i := range s{
if len(s[i].Application) > 0{
clientMap := map[string]interface{}{
"instance-id":s[i].Name,
"Division":s[i].Division,
"Status":2,
"Application":s[i].Application,
"checks":make(map[string]interface{}),
}
clients[s[i].Application]=append(clients[s[i].Application],clientMap)
resultChannel <- clientMap
}
}
am not sure what is the best way to handle such case . at the end i need to have the following structure :
[
application_name[instance-id:xxxxx Division:xxxxx Status:2 Application:fulfilment checks:[xxxxx:0,yyyy:1]]
application_name[instance-id:xxxxx Division:xxxxx Status:2 Application:fulfilment checks:[xxxxx:0,yyyy:1]]
]
Create a struct type to represent the data and use that struct instead of interface{} in the map data types. There is no need to use interface{} in this case.
For example:
type Client struct {
InstanceId string
Division string
Status int
Application string
Checks map[string]int
}