首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从地图中访问图中的值

从地图中访问图中的值
EN

Stack Overflow用户
提问于 2022-04-25 11:46:36
回答 1查看 109关注 0票数 -1

我正在利用Avi Go SDK获取avi健康监视器配置,如下所示

代码语言:javascript
复制
var healthmonitormap map[string]interface{}
err = aviClient.AviSession.GetObjectByName("healthmonitor", "healthmonitorname", &healthmonitormap)
if err != nil {
    t.Error(err)
}

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

代码语言:javascript
复制
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等,如下所示

代码语言:javascript
复制
assert.Equal(t, healthmonitormap["name"], "helloworldspringbootssl-dit1-monitor")
assert.Equal(t, float64(443), healthmonitormap["monitor_port"])

但是,我无法理解如何访问地图中的映射,如http_request和http_response_code。

如能提供任何协助,我们将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-25 12:41:56

由于映射的elem是一个interface{},所以必须将其转换为相应的类型才能以映射的形式访问它,如下所示:

代码语言:javascript
复制
if value, ok := healthmonitormap["https_monitor"].(map[string]interface{}); ok {
    fmt.Println(value["http_request"]) //Output: GET /welcome HTTP/1.1 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71999022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档