四、interface

(1) interface的赋值问题

以下代码能编译过去吗?为什么?

test12.go

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type People interface {
  6. Speak(string) string
  7. }
  8. type Student struct{}
  9. func (stu *Student) Speak(think string) (talk string) {
  10. if think == "love" {
  11. talk = "You are a good boy"
  12. } else {
  13. talk = "hi"
  14. }
  15. return
  16. }
  17. func main() {
  18. var peo People = Student{}
  19. think := "love"
  20. fmt.Println(peo.Speak(think))
  21. }

继承与多态的特点

在golang中对多态的特点体现从语法上并不是很明显。

我们知道发生多态的几个要素:

1、有interface接口,并且有接口定义的方法。

2、有子类去重写interface的接口。

3、有父类指针指向子类的具体对象

那么,满足上述3个条件,就可以产生多态效果,就是,父类指针可以调用子类的具体方法。

所以上述代码报错的地方在var peo People = Student{}这条语句, Student{}已经重写了父类People{}中的Speak(string) string方法,那么只需要用父类指针指向子类对象即可。

所以应该改成var peo People = &Student{} 即可编译通过。(People为interface类型,就是指针类型)

(2) interface的内部构造(非空接口iface情况)

以下代码打印出来什么内容,说出为什么。

test14.go

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type People interface {
  6. Show()
  7. }
  8. type Student struct{}
  9. func (stu *Student) Show() {
  10. }
  11. func live() People {
  12. var stu *Student
  13. return stu
  14. }
  15. func main() {
  16. if live() == nil {
  17. fmt.Println("AAAAAAA")
  18. } else {
  19. fmt.Println("BBBBBBB")
  20. }
  21. }

结果

  1. BBBBBBB

分析:

我们需要了解interface的内部结构,才能理解这个题目的含义。

interface在使用的过程中,共有两种表现形式

一种为空接口(empty interface),定义如下:

  1. var MyInterface interface{}

另一种为非空接口(non-empty interface), 定义如下:

  1. type MyInterface interface {
  2. function()
  3. }

这两种interface类型分别用两种struct表示,空接口为eface, 非空接口为iface. 四、interface - 图1


空接口eface

空接口eface结构,由两个属性构成,一个是类型信息_type,一个是数据信息。其数据结构声明如下:

  1. type eface struct { //空接口
  2. _type *_type //类型信息
  3. data unsafe.Pointer //指向数据的指针(go语言中特殊的指针类型unsafe.Pointer类似于c语言中的void*)
  4. }

_type属性:是GO语言中所有类型的公共描述,Go语言几乎所有的数据结构都可以抽象成 _type,是所有类型的公共描述,type负责决定data应该如何解释和操作,type的结构代码如下:

  1. type _type struct {
  2. size uintptr //类型大小
  3. ptrdata uintptr //前缀持有所有指针的内存大小
  4. hash uint32 //数据hash值
  5. tflag tflag
  6. align uint8 //对齐
  7. fieldalign uint8 //嵌入结构体时的对齐
  8. kind uint8 //kind 有些枚举值kind等于0是无效的
  9. alg *typeAlg //函数指针数组,类型实现的所有方法
  10. gcdata *byte
  11. str nameOff
  12. ptrToThis typeOff
  13. }

data属性: 表示指向具体的实例数据的指针,他是一个unsafe.Pointer类型,相当于一个C的万能指针void*

四、interface - 图2


非空接口iface

iface 表示 non-empty interface 的数据结构,非空接口初始化的过程就是初始化一个iface类型的结构,其中data的作用同eface的相同,这里不再多加描述。

  1. type iface struct {
  2. tab *itab
  3. data unsafe.Pointer
  4. }

iface结构中最重要的是itab结构(结构如下),每一个 itab 都占 32 字节的空间。itab可以理解为pair<interface type, concrete type> 。itab里面包含了interface的一些关键信息,比如method的具体实现。

  1. type itab struct {
  2. inter *interfacetype // 接口自身的元信息
  3. _type *_type // 具体类型的元信息
  4. link *itab
  5. bad int32
  6. hash int32 // _type里也有一个同样的hash,此处多放一个是为了方便运行接口断言
  7. fun [1]uintptr // 函数指针,指向具体类型所实现的方法
  8. }

其中值得注意的字段,个人理解如下:

  1. interface type包含了一些关于interface本身的信息,比如package path,包含的method。这里的interfacetype是定义interface的一种抽象表示。
  2. type表示具体化的类型,与eface的 type类型相同。
  3. hash字段其实是对_type.hash的拷贝,它会在interface的实例化时,用于快速判断目标类型和接口中的类型是否一致。另,Go的interface的Duck-typing机制也是依赖这个字段来实现。
  4. fun字段其实是一个动态大小的数组,虽然声明时是固定大小为1,但在使用时会直接通过fun指针获取其中的数据,并且不会检查数组的边界,所以该数组中保存的元素数量是不确定的。

四、interface - 图3


所以,People拥有一个Show方法的,属于非空接口,People的内部定义应该是一个iface结构体

  1. type People interface {
  2. Show()
  3. }

四、interface - 图4

  1. func live() People {
  2. var stu *Student
  3. return stu
  4. }

stu是一个指向nil的空指针,但是最后return stu 会触发匿名变量 People = stu值拷贝动作,所以最后live()放回给上层的是一个People insterface{}类型,也就是一个iface struct{}类型。 stu为nil,只是iface中的data 为nil而已。 但是iface struct{}本身并不为nil.

四、interface - 图5

所以如下判断的结果为BBBBBBB

  1. func main() {
  2. if live() == nil {
  3. fmt.Println("AAAAAAA")
  4. } else {
  5. fmt.Println("BBBBBBB")
  6. }
  7. }

(3) interface内部构造(空接口eface情况)

下面代码结果为什么?

  1. func Foo(x interface{}) {
  2. if x == nil {
  3. fmt.Println("empty interface")
  4. return
  5. }
  6. fmt.Println("non-empty interface")
  7. }
  8. func main() {
  9. var p *int = nil
  10. Foo(p)
  11. }

结果

  1. non-empty interface

分析

不难看出,Foo()的形参x interface{}是一个空接口类型eface struct{}

四、interface - 图6

在执行Foo(p)的时候,触发x interface{} = p语句,所以此时 x结构如下。 四、interface - 图7

所以 x 结构体本身不为nil,而是data指针指向的p为nil。


(4) inteface{}与*interface{}

ABCD中哪一行存在错误?

test15.go

  1. type S struct {
  2. }
  3. func f(x interface{}) {
  4. }
  5. func g(x *interface{}) {
  6. }
  7. func main() {
  8. s := S{}
  9. p := &s
  10. f(s) //A
  11. g(s) //B
  12. f(p) //C
  13. g(p) //D
  14. }

结果

  1. BD两行错误
  2. B错误为: cannot use s (type S) as type *interface {} in argument to g:
  3. *interface {} is pointer to interface, not interface
  4. D错误为:cannot use p (type *S) as type *interface {} in argument to g:
  5. *interface {} is pointer to interface, not interface

看到这道题需要第一时间想到的是Golang是强类型语言,interface是所有golang类型的父类 函数中func f(x interface{})interface{}可以支持传入golang的任何类型,包括指针,但是函数func g(x *interface{})只能接受*interface{}