golang access values from map inside a map

huangapple go评论132阅读模式
英文:

golang access values from map inside a map

问题

我正在使用Avi Go SDK来获取 Avi 健康监控配置,代码如下所示:

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

这段代码将健康监控的元数据存储在 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]

从这个映射中,我能够成功访问 namemonitor_port 等根级别的属性,代码如下:

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

然而,我不知道如何访问 http_requesthttp_response_code 这样的嵌套映射。它们是映射中的映射。

如果有任何帮助,将不胜感激。

英文:

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.

答案1

得分: 2

由于地图的元素是interface{},您需要将其转换为相应的类型,以便将其作为地图访问,就像这样:

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

这段代码将healthmonitormap["https_monitor"]转换为map[string]interface{}类型,并检查转换是否成功。如果成功,它将访问value["http_request"]并打印输出结果。

英文:

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 
}

huangapple
  • 本文由 发表于 2022年4月25日 19:46:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/71999022.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定