概述
如果说goroutine和channel是go并发的两大基石,那么接口是go语言编程中数据类型的关键。在go语言的实际编程中,几乎所有的数据结构都围绕接口展开,接口是go语言中所有数据结构的核心。
go语言中的接口是一些方法的集合(method set),它指定了对象的行为:如果它(任何数据类型)可以做这些事情,那么它就可以在这里使用。
接口的定义和使用
比如
好了,我的一个接口实现了这个接口:
go中interface的写法
下面看几个interface的例子:
func somefunction(w interface{write(string)}){
w.write("pizza")
}
(1)通过对象实例赋值
将一个对象实例赋值给一个接口之前,要保证该对象实现了接口的所有方法。考虑如下示例:
type lessadder interface {
less(b integer) bool
add(b integer)
}
var a integer = 1
var b1 lessadder = &a //ok
var b2 lessadder = a //not ok
the method set of any other named type t consists of all methods with receiver type t. the method set of the corresponding pointer type t is the set of all methods with receiver t or t (that is, it also contains the method set of t).
也就是说*integer实现了接口lessadder的所有方法,而integer只实现了less方法,所以不能赋值。
(2)通过接口赋值
var r io.reader = new(os.file)
var rw io.readwriter = r //not ok
var rw2 io.readwriter = new(os.file)
var r2 io.reader = rw2 //ok
接口嵌套
我们来看看io package中的另外一个接口:
// readwriter is the interface that groups the basic read and write methods.
type readwriter interface {
reader
writer
}
type readwriter interface {
read(p []byte) (n int, err error)
write(p []byte) (n int, err error)
}
// illegal: bad cannot embed itself
type bad interface {
bad
}
// illegal: bad1 cannot embed itself using bad2
type bad1 interface {
bad2
}
type bad2 interface {
bad1
}
空接口(empty interface)
空接口比较特殊,它不包含任何方法:
var v1 interface{} = 1
var v2 interface{} = "abc"
var v3 interface{} = struct{ x int }{1}
func fprint(w io.writer, a ...interface{}) (n int, err error)
func fprintf(w io.writer, format string, a ...interface{})
func fprintln(w io.writer, a ...interface{})
func print(a ...interface{}) (n int, err error)
func printf(format string, a ...interface{})
func println(a ...interface{}) (n int, err error)
t := []int{1, 2, 3, 4}
var s []interface{} = t
cannot use t (type []int) as type []interface {} in assignment
我们必须通过下面这种方式:
t := []int{1, 2, 3, 4}
s := make([]interface{}, len(t))
for i, v := range t {
s[i] = v
}
如对本文有疑问, 点击进行留言回复!!
网友评论