[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{}

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

 例如下面这样的错误:

开源作品

GO-FLY,一套可私有化部署的免费开源客服系统,安装过程不超过五分钟(超过你打我 !),基于Golang开发,二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的网页在线客服系统,致力于帮助广大开发者/中小站长快速整合私有客服功能
github地址:go-fly
官网地址:https://gofly.sopans.com

赞赏作者

微信交流

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