Last active
October 18, 2019 07:05
-
-
Save tqcenglish/c30cd7fa669151167ebb8dddd34df690 to your computer and use it in GitHub Desktop.
遍历 map[string]interface{} 类型
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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