繁体   English   中英

map 中的 map 的 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)
}

它将健康监视器的元数据存储在 healthmonitormap 中,如下所示为 map

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]

从 map,我能够成功访问 name、monitor_port 等,它们位于 map 的根目录中,如下所示

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

但是我无法理解如何访问 map 中的 map 之类的 http_request 和 http_response_code。

任何帮助表示赞赏。

由于 map 的元素是一个interface{} ,您必须将其转换为相应的类型才能将其作为 map 进行访问,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM