Skip to content

Instantly share code, notes, and snippets.

@tqcenglish
Last active October 18, 2019 07:05
Show Gist options
  • Save tqcenglish/c30cd7fa669151167ebb8dddd34df690 to your computer and use it in GitHub Desktop.
Save tqcenglish/c30cd7fa669151167ebb8dddd34df690 to your computer and use it in GitHub Desktop.
遍历 map[string]interface{} 类型
package main
import "fmt"
// PrintJSON 遍历 map[string]interface{} 类型
func PrintJSON(m map[string]interface{}) {
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case float64:
fmt.Println(k, "is float", int64(vv))
case int:
fmt.Println(k, "is int", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
case nil:
fmt.Println(k, "is nil", "null")
case map[string]interface{}:
fmt.Println(k, "is an map:")
PrintJSON(vv)
default:
fmt.Println(k, "is of a type I don't know how to handle ", fmt.Sprintf("%T", v))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment