初步解读Golang中的接口相关编写方法

2020-01-28 12:02:25王振洲

概述
如果说goroutine和channel是Go并发的两大基石,那么接口是Go语言编程中数据类型的关键。在Go语言的实际编程中,几乎所有的数据结构都围绕接口展开,接口是Go语言中所有数据结构的核心。
Go语言中的接口是一些方法的集合(method set),它指定了对象的行为:如果它(任何数据类型)可以做这些事情,那么它就可以在这里使用。

接口的定义和使用

比如


type I interface{
    Get() int
    Put(int)
 
}


这段话就定义了一个接口,它包含两个函数Get和Put

好了,我的一个接口实现了这个接口:


type S struct {val int}
func (this *S) Get int {
    return this.val
}
func (this *S)Put(v int) {
    this.val = v
 
}


这个结构S就是实现了接口I

Go中interface的写法

下面看几个interface的例子:


func SomeFunction(w interface{Write(string)}){
    w.Write("pizza")
 
}


这个例子中,直接将interface定义在参数中,很特别…


func weirdFunc( i int ) interface{} {
  if i ==  0 {
    return "zero"
  }
  return i;
}


接口赋值
我们可以将一个实现接口的对象实例赋值给接口,也可以将另外一个接口赋值给接口。

(1)通过对象实例赋值

将一个对象实例赋值给一个接口之前,要保证该对象实现了接口的所有方法。考虑如下示例:


type Integer int
func (a Integer) Less(b Integer) bool {
 return a < b
}
func (a *Integer) Add(b Integer) {
 *a += b
}

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


b2的赋值会报编译错误,为什么呢?还记得<类型方法>一章中讨论的Go语言规范的规定吗?

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)