解析(map[string]interface{})数据格式并打印出数据

前端之家收集整理的这篇文章主要介绍了解析(map[string]interface{})数据格式并打印出数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

学习golang 记录下

//解析(map[string]interface{})数据格式并打印出数据

func print_json(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:")
            print_json(vv)
        default:
            fmt.Println(k,"is of a type I don't know how to handle ",fmt.Sprintf("%T",v))
        }
    }
}
代码有些丑陋就不发表在代码中了。

猜你在找的Go相关文章