golang 打印出map[string]interface{} 中所有的数据和数据类型
可以使用range循环遍历map[string]interface{}中的所有键值对,然后使用switch语句判断值的类型并进行打印。
以下是示例代码:
m := map[string]interface{}{
"name": "John",
"age": 30,
"score": 90.5,
"valid": true,
}
for k, v := range m {
fmt.Printf("%s: ", k)
switch val := v.(type) {
case string:
fmt.Printf("%s (string)\n", val)
case int:
fmt.Printf("%d (int)\n", val)
case float64:
fmt.Printf("%f (float)\n", val)
case bool:
fmt.Printf("%t (bool)\n", val)
default:
fmt.Printf("%v (%T)\n", val, val)
}
}
输出结果:
name: John (string)
age: 30 (int)
score: 90.500000 (float)
valid: true (bool)
原文地址: https://www.cveoy.top/t/topic/uy4 著作权归作者所有。请勿转载和采集!