interface{} 与 []interface{}
来源:互联网 发布:tower 办公软件 编辑:程序博客网 时间:2024/06/09 18:18
原文
简介
基于可以对interface{}赋值任何类型的变量,很多人会尝试如下的代码:
var dataSlice []int = foo()var interfaceSlice []interface{} = dataSlice
很不幸会导致错误:
cannot use dataSlice (type []int) as type []interface { } in assignment
于是问题来了: 为什么可以把任何类型赋值给interface{},却不能把任何类型的切片赋值到[]interface{}?
原因
导致这个问题,主要有两个原因。
[]interface{}类型**不是**interface{}类型, 它是一个切片,切片元素的类型恰好是interface{}。但即便这样解释,有人可能仍然觉得前面的用法没毛病。
真的没毛病?[]interface{}类型变量拥有特定的内存结构,这在编译时就已经决定了。
每个interface{}占两个字(word),一个字用于存放interface存放的类型,另一个字用于存放实际数据或者是指向数据的指针。于是长度为N的[]interface{}类型切片背后是一个N*2字长的一块数据。
这与一般的[]MyType类型切片不同,相同长度的[]MyType切片背后的数据块大小为N*sizeof(MyType)字长。
正是这两个原因决定了不能直接将某些[]MyType切片赋值给[]interface{}, 他们背后代表的数据意义不同。
如何使用
实际上这取决于你的需求。
如果你想得到一个元素为任意类型的列表的容器,并且在索引其中元素之前会把它转换为原本的数据类型,可以直接使用interface{}即可。这种方式很通用(非编译时类型安全:猜测原文作者是想表达被转换为interface{}类型的数据,转换回去也必须是原来的类型,否则编译会报错)也很快速。
参考代码:
package mainimport ( "fmt")type Person interface { SayHi() interface{} // 如果此方法返回值换成[]interface{} 会报错}type Man struct { Name string}func (m Man) SayHi() interface{} { return []byte("Hi, my name is: " + m.Name)}type Women struct { Name string}func (w Women) SayHi() interface{} { return []byte("Hi, my name is: " + w.Name)}func sayHi(p Person) { fmt.Println(string(p.SayHi().([]byte))) // 转换为[]byte类型}func main() { m := Man{Name: "Bruce"} w := Women{ Name: "Anna", } sayHi(m) sayHi(w)}
但如果你需要[]interface{},因为你会在将数据类型转换为原本类型之前索引其中元素,或者你使用某种接口类型需要调用它的方法,那就需要逐个copy得到[]interface{}类型的切片:
var dataSlice []int = foo()var interfaceSlice []interface{} = make([]interface{}, len(dataSlice))for i, d := range dataSlice { interfaceSlice[i] = d}
- interface{} 与 []interface{}
- Interface
- interface
- interface
- interface
- interface
- interface
- @interface
- Interface
- @interface
- Interface
- interface
- Interface
- interface
- Interface
- Interface
- interface
- interface
- 最常用的 15 大 Eclipse 开发快捷键技巧
- 输入框弹出输入法高度自适应(遮挡键盘,自动反弹)
- DSP关键字cregister (关于IER,IFR找不到定义)
- windows安装完nodejs后,cmd输入npm没反应啊 就光标一直闪 node是正常的
- Git gitignore
- interface{} 与 []interface{}
- ZooKeeper之(七)应用实例
- HDU6129-Just do it
- java学习路线
- 2017年8月17日提高组T1 游戏
- 倒三角形(triangle)
- GitHub之多人开发一个项目
- transactional放置在service层还是controller层
- 第7章 暴力求解法 例7-1 除法(Division, UVa 725)