golang 从地图内部的地图访问值
golang access values from map inside a map
我正在利用 Avi Go SDK 获取 avi healthmonitor 配置,如下所示
var healthmonitormap map[string]interface{}
err = aviClient.AviSession.GetObjectByName("healthmonitor", "healthmonitorname", &healthmonitormap)
if err != nil {
t.Error(err)
}
将healthmonitor的元数据存储在healthmonitormap中,如下图
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]
从地图上,我能够成功访问名称,monitor_port 等,它们位于地图的根目录中,如下所示
assert.Equal(t, healthmonitormap["name"], "helloworldspringbootssl-dit1-monitor")
assert.Equal(t, float64(443), healthmonitormap["monitor_port"])
但是我无法理解如何访问地图中的地图 http_request 和 http_response_code 之类的东西。
感谢任何帮助。
由于地图的元素是 interface{}
你必须将它转换为相应的类型才能将其作为地图访问,如下所示:
if value, ok := healthmonitormap["https_monitor"].(map[string]interface{}); ok {
fmt.Println(value["http_request"]) //Output: GET /welcome HTTP/1.1
}
我正在利用 Avi Go SDK 获取 avi healthmonitor 配置,如下所示
var healthmonitormap map[string]interface{}
err = aviClient.AviSession.GetObjectByName("healthmonitor", "healthmonitorname", &healthmonitormap)
if err != nil {
t.Error(err)
}
将healthmonitor的元数据存储在healthmonitormap中,如下图
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]
从地图上,我能够成功访问名称,monitor_port 等,它们位于地图的根目录中,如下所示
assert.Equal(t, healthmonitormap["name"], "helloworldspringbootssl-dit1-monitor")
assert.Equal(t, float64(443), healthmonitormap["monitor_port"])
但是我无法理解如何访问地图中的地图 http_request 和 http_response_code 之类的东西。
感谢任何帮助。
由于地图的元素是 interface{}
你必须将它转换为相应的类型才能将其作为地图访问,如下所示:
if value, ok := healthmonitormap["https_monitor"].(map[string]interface{}); ok {
fmt.Println(value["http_request"]) //Output: GET /welcome HTTP/1.1
}