首页 > 其他 > 详细

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

时间:2021-04-28 22:10:50      阅读:26      评论:0      收藏:0      [点我收藏+]

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

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

 例如下面这样的错误:

技术分享图片

 

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

原文:https://www.cnblogs.com/taoshihan/p/14715499.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!