空接口 interface{}

可以存储任何类型的数据

但是在和slice以及map配合时 ,要注意

[]interface{}  或者 map[string]interface{}

 

可能会犯这样的错误 cannot use (type []string) as type []interface {}

不能将[]T 转成 []interface  , 也不能将 map[string]T 转成 map[string]interface{}

 

Go语言规范不允许这样做,因为两种类型在内存中没有相同的表现形式。

需要单独定义[]interface{}  map[string]interface{}

把想转换的元素复制一遍到上面的类型里

 例如下面这样的错误:

 

文章链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。