[Go] 解决空接口 interface{} cannot use (type []string) as type []interface {}


空接口 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{}

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

 例如下面这样的错误:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM