时间:2021-04-06 10:01:54 | 栏目:Golang | 点击:次
概述
如果说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
}