簡體   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