知識(shí)
interface{} 可以代表任意類型
interface{} 就是一個(gè)空接口,所有類型都實(shí)現(xiàn)了這個(gè)接口谢鹊,所以它可以代表所有類型
代碼
https://github.com/fengchunjian/goexamples/tree/master/map_interface
//null_interface.go
package main
import "fmt"
func main() {
m := make(map[string]interface{})
m["int"] = 123
m["string"] = "hello"
m["bool"] = true
for _, v := range m {
switch v.(type) {
case string:
fmt.Println(v, "is string")
case int:
fmt.Println(v, "is int")
default:
fmt.Println(v, "is other")
}
}
fmt.Println(m)
}
編譯運(yùn)行
go build null_interface.go
./null_interface
123 is int
hello is string
true is other
map[int:123 string:hello bool:true]
參考文檔
Go語言嵌套Map類型 http://blog.ninja911.com/blog-show-blog_id-76.html
解析(map[string]interface{})數(shù)據(jù)格式并打印出數(shù)據(jù) http://www.codeweblog.com/%E8%A7%A3%E6%9E%90-map-string-interface-%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F%E5%B9%B6%E6%89%93%E5%8D%B0%E5%87%BA%E6%95%B0%E6%8D%AE/