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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-30
  • 2022-02-26
  • 2021-11-20
  • 2022-01-22
  • 2021-11-06
猜你喜欢
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-02
  • 2022-12-23
相关资源
相似解决方案