Golang 中获取 Topics 数组中 num 数据的示例
Topics := []interface{}{
map[string]interface{}{
'topic': '$share/' + global.GVA_CONFIG.System.Identification + '_jiminate3_status/' + global.GVA_CONFIG.System.Identification + '/jiminate3/+/status',
'qos': 0,
'num': 2,
},
map[string]interface{}{
'topic': '$share/' + global.GVA_CONFIG.System.Identification + '_jiminate3_request/' + global.GVA_CONFIG.System.Identification + '/jiminate3/+/request/+',
'qos': 0,
'num': 2,
},
map[string]interface{}{
'topic': '$share/' + global.GVA_CONFIG.System.Identification + '_jiminate3_response/' + global.GVA_CONFIG.System.Identification + '/jiminate3/+/response/+',
'qos': 0,
'num': 2,
},
map[string]interface{}{
'topic': '$share/' + global.GVA_CONFIG.System.Identification + '_jiminate3_report/' + global.GVA_CONFIG.System.Identification + '/jiminate3/+/report',
'qos': 0,
'num': 2,
},
}
如何获取里面的 num 数据内容:
可以通过遍历 Topics 数组,然后使用类型断言来获取每个元素中的 num 值,示例代码如下:
for _, topic := range Topics {
if t, ok := topic.(map[string]interface{}); ok {
num := t['num']
fmt.Println(num)
}
}
原文地址: https://www.cveoy.top/t/topic/lDT8 著作权归作者所有。请勿转载和采集!