Home > Mobile >  golang access values from map inside a map
golang access values from map inside a map

Time:04-30

I am leveraging Avi Go SDK to fetch avi healthmonitor configuration as below

var healthmonitormap map[string]interface{}
err = aviClient.AviSession.GetObjectByName("healthmonitor", "healthmonitorname", &healthmonitormap)
if err != nil {
    t.Error(err)
}

Which store the metadata of health monitor in healthmonitormap as a map like below

map[
_last_modified:1644392621383838 
failed_checks:3 
https_monitor:map[
    http_request:GET /welcome HTTP/1.1 
    http_response_code:[HTTP_2XX HTTP_3XX]] 
monitor_port:443 
name:helloworldspringbootssl-dit1-monitor 
receive_timeout:3 
send_interval:10 
successful_checks:3 
tenant_ref:https://xxxxxxxxxxxxxxxx/api/tenant/tenant-xxxxxxxxxxxx 
type:HEALTH_MONITOR_HTTPS 
url:https://xxxxxxxxxxx/api/healthmonitor/healthmonitor-xxxxxxxxxxxxxxx 
uuid:healthmonitor-xxxxxxxxxxxxxxxx]

From the map, I am able to successfully access name, monitor_port etc. Which are in the root of map as below

assert.Equal(t, healthmonitormap["name"], "helloworldspringbootssl-dit1-monitor")
assert.Equal(t, float64(443), healthmonitormap["monitor_port"])

However I am not able to understand how to access the things like http_request and http_response_code which are map inside a map.

Any assistance is appreciated.

CodePudding user response:

Since the elem of the map is an interface{} you have to convert it to the corresponding type to access it as a map, like this:

if value, ok := healthmonitormap["https_monitor"].(map[string]interface{}); ok {
    fmt.Println(value["http_request"]) //Output: GET /welcome HTTP/1.1 
}
  • Related