GoLang接口---中

x33g5p2x  于2022-08-17 转载在 其他  
字(7.3k)|赞(0)|评价(0)|浏览(627)

引言

GoLang接口—上

上一篇文章中,我们对接口的基本使用和底层实现做了简单的了解,本文对接口的一些使用技巧做相关陈述。

接口的类型断言

一个接口类型的变量 varI 中可以包含任何类型的值,必须有一种方式来检测它的 动态 类型,即运行时在变量中存储的值的实际类型。

  1. func main() {
  2. var shaper Shaper
  3. if rand.Intn(10) > 5 {
  4. shaper = Circle{radius: 12.1}
  5. } else {
  6. shaper = Square{side: 12.1}
  7. }
  8. println(shaper)
  9. }

显然,上面这段程序,只有在运行时才能确定shaper接口中存储的值的类型

那么,有没有什么方法可以来判断当前shaper接口中存储的值的类型到底是什么呢?

通常我们可以使用 类型断言 来测试在某个时刻 varI 是否包含类型 T 的值:

  1. v := varI.(T) // unchecked type assertion

varI 必须是一个接口变量,否则编译器会报错:

  1. invalid type assertion: varI.(T) (non-interface type (type of varI) on left)

类型断言可能是无效的,虽然编译器会尽力检查转换是否有效,但是它不可能预见所有的可能性。如果转换在程序运行时失败会导致错误发生。更安全的方式是使用以下形式来进行类型断言:

  1. if v, ok := varI.(T); ok { // checked type assertion
  2. Process(v)
  3. return
  4. }
  5. // varI is not of type T

如果转换合法,vvarI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,okfalse,也没有运行时错误发生。

这里 接口变量.(接口实现类的类型) 的操作可以理解为将父类类型强制转换为子类类型后返回,但是转换的前提是,实现类必须实现了当前接口的所有方法才行,否则go编译会报错

实例演示

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Square struct {
  6. side float32
  7. }
  8. func (sq Square) Area() float32 {
  9. return sq.side * sq.side
  10. }
  11. type Shaper interface {
  12. Area() float32
  13. }
  14. func main() {
  15. var shaper Shaper
  16. //调用方法为结构体---此时接口中保存的值类型为结构体
  17. shaper = Square{side: 250}
  18. testValueType(shaper)
  19. testPointerType(shaper)
  20. //调用方为指针--此时接口中保存的值类型为指针
  21. shaper = &Square{side: 520}
  22. testValueType(shaper)
  23. testPointerType(shaper)
  24. }
  25. func testValueType(shaper Shaper) {
  26. println("Test ValueType...")
  27. //判断接口中保存的值类型是否为结构体
  28. if square, ok := shaper.(Square); ok {
  29. fmt.Println("当前Shaper接口中保存的值类型为Square, ", square)
  30. } else {
  31. fmt.Println("当前Shaper接口中保存的值类型不是Square")
  32. }
  33. fmt.Println("-----------------------------------------------")
  34. }
  35. func testPointerType(shaper Shaper) {
  36. println("Test PointerType...")
  37. //判断接口中保存的值类型是否为指针
  38. if square, ok := shaper.(*Square); ok {
  39. fmt.Println("当前Shaper接口中保存的值类型为Square, ", square)
  40. } else {
  41. fmt.Println("当前Shaper接口中保存的值类型不是Square")
  42. }
  43. fmt.Println("-----------------------------------------------")
  44. }

打印输出结果的具体原因,可以参考下面这幅图思考:

因为对于接口而言,当方法接受者为指针时,只有指针才能调用该方法,因此如果将上面的例子改一下:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type Square struct {
  6. side float32
  7. }
  8. //方法接受者为指针,只有指针实现才能调用该方法
  9. func (sq *Square) Area() float32 {
  10. return sq.side * sq.side
  11. }
  12. type Shaper interface {
  13. Area() float32
  14. }
  15. func main() {
  16. var shaper Shaper
  17. //这里就无法结构体实现的Square就没有实现Shaper接口了
  18. //shaper = Square{side: 250}
  19. //
  20. //testValueType(shaper)
  21. //testPointerType(shaper)
  22. shaper = &Square{side: 520}
  23. //testValueType(shaper)
  24. testPointerType(shaper)
  25. }
  26. //func testValueType(shaper Shaper) {
  27. // println("Test ValueType...")
  28. //这里Square结构体并没有实现Shaper接口,因此不能直接这样进行强制类型转换
  29. // if square, ok := shaper.(Square); ok {
  30. // fmt.Println("当前Shaper接口中保存的值类型为Square, ", square)
  31. // } else {
  32. // fmt.Println("当前Shaper接口中保存的值类型不是Square")
  33. // }
  34. //
  35. // fmt.Println("-----------------------------------------------")
  36. //}
  37. func testPointerType(shaper Shaper) {
  38. println("Test PointerType...")
  39. if square, ok := shaper.(*Square); ok {
  40. fmt.Println("当前Shaper接口中保存的值类型为Square, ", square)
  41. } else {
  42. fmt.Println("当前Shaper接口中保存的值类型不是Square")
  43. }
  44. fmt.Println("-----------------------------------------------")
  45. }

类型判断:type-switch

接口变量的类型也可以使用一种特殊形式的 switch 来检测:type-switch

  1. switch t := areaIntf.(type) {
  2. case *Square:
  3. fmt.Printf("Type Square %T with value %v\n", t, t)
  4. case *Circle:
  5. fmt.Printf("Type Circle %T with value %v\n", t, t)
  6. case nil:
  7. fmt.Printf("nil value: nothing to check?\n")
  8. default:
  9. fmt.Printf("Unexpected type %T\n", t)
  10. }

变量 t 得到了 areaIntf 的值和类型, 所有 case 语句中列举的类型(nil 除外)都必须实现对应的接口(在上例中即 Shaper),如果被检测类型没有在 case 语句列举的类型中,就会执行 default 语句。

可以用 type-switch 进行运行时类型分析,但是在 type-switch 不允许有 fallthrough 。

如果仅仅是测试变量的类型,不用它的值,那么就可以不需要赋值语句,比如:

  1. switch areaIntf.(type) {
  2. case *Square:
  3. // TODO
  4. case *Circle:
  5. // TODO
  6. ...
  7. default:
  8. // TODO
  9. }

nil 和 non-nil

我们可以通过一个例子理解『Go 语言的接口类型不是任意类型』这一句话,下面的代码在 main 函数中初始化了一个 *TestStruct 结构体指针,由于指针的零值是 nil,所以变量 s 在初始化之后也是 nil:

  1. package main
  2. type TestStruct struct{}
  3. func NilOrNot(v interface{}) bool {
  4. return v == nil
  5. }
  6. func main() {
  7. var s *TestStruct
  8. fmt.Println(s == nil) // #=> true
  9. fmt.Println(NilOrNot(s)) // #=> false
  10. }
  11. $ go run main.go
  12. true
  13. false

我们简单总结一下上述代码执行的结果:

  1. 将上述变量与 nil 比较会返回 true;
  2. 将上述变量传入 NilOrNot 方法并与 nil 比较会返回 false;

出现上述现象的原因是调用 NilOrNot 函数时发生了隐式的类型转换,除了向方法传入参数之外,变量的赋值也会触发隐式类型转换。在类型转换时,*TestStruct 类型会转换成 interface{} 类型,转换后的变量不仅包含转换前的变量,还包含变量的类型信息 TestStruct,所以转换后的变量与 nil 不相等。

空接口

空接口或者最小接口 不包含任何方法,它对实现不做任何要求:

  1. type Any interface {}

任何其他类型都实现了空接口(它不仅仅像 Java/C# 中 Object 引用类型),any 或 Any 是空接口一个很好的别名或缩写。

空接口类似 Java/C# 中所有类的基类: Object 类,二者的目标也很相近。

可以给一个空接口类型的变量 var val interface {} 赋任何类型的值。

示例:

  1. package main
  2. import "fmt"
  3. var i = 5
  4. var str = "ABC"
  5. type Person struct {
  6. name string
  7. age int
  8. }
  9. type Any interface{}
  10. func main() {
  11. var val Any
  12. val = 5
  13. fmt.Printf("val has the value: %v\n", val)
  14. val = str
  15. fmt.Printf("val has the value: %v\n", val)
  16. pers1 := new(Person)
  17. pers1.name = "Rob Pike"
  18. pers1.age = 55
  19. val = pers1
  20. fmt.Printf("val has the value: %v\n", val)
  21. switch t := val.(type) {
  22. case int:
  23. fmt.Printf("Type int %T\n", t)
  24. case string:
  25. fmt.Printf("Type string %T\n", t)
  26. case bool:
  27. fmt.Printf("Type boolean %T\n", t)
  28. case *Person:
  29. fmt.Printf("Type pointer to Person %T\n", t)
  30. default:
  31. fmt.Printf("Unexpected type %T", t)
  32. }
  33. }

输出:

  1. val has the value: 5
  2. val has the value: ABC
  3. val has the value: &{Rob Pike 55}
  4. Type pointer to Person *main.Person

在上面的例子中,接口变量 val 被依次赋予一个 int,string 和 Person 实例的值,然后使用 type-switch 来测试它的实际类型。每个 interface {} 变量在内存中占据两个字长:一个用来存储它包含的类型,另一个用来存储它包含的数据或者指向数据的指针。

构建通用类型或包含不同类型变量的数组

通过使用空接口。让我们给空接口定一个别名类型 Element:type Element interface{}

然后定义一个容器类型的结构体 Vector,它包含一个 Element 类型元素的切片:

  1. type Vector struct {
  2. a []Element
  3. }

Vector 里能放任何类型的变量,因为任何类型都实现了空接口,实际上 Vector 里放的每个元素可以是不同类型的变量。我们为它定义一个 At() 方法用于返回第 i 个元素:

  1. func (p *Vector) At(i int) Element {
  2. return p.a[i]
  3. }

再定一个 Set() 方法用于设置第 i 个元素的值:

  1. func (p *Vector) Set(i int, e Element) {
  2. p.a[i] = e
  3. }

Vector 中存储的所有元素都是 Element 类型,要得到它们的原始类型(unboxing:拆箱)需要用到类型断言。

TODO:The compiler rejects assertions guaranteed to fail,类型断言总是在运行时才执行,因此它会产生运行时错误。

复制数据切片至空接口切片

假设你有一个 myType 类型的数据切片,你想将切片中的数据复制到一个空接口切片中,类似:

  1. var dataSlice []myType = FuncReturnSlice()
  2. var interfaceSlice []interface{} = dataSlice

可惜不能这么做,编译时会出错:cannot use dataSlice (type []myType) as type []interface { } in assignment。

原因是它们俩在内存中的布局是不一样的。

必须使用 for-range 语句来一个一个显式地赋值:

  1. var dataSlice []myType = FuncReturnSlice()
  2. var interfaceSlice []interface{} = make([]interface{}, len(dataSlice))
  3. for i, d := range dataSlice {
  4. interfaceSlice[i] = d
  5. }

通用类型的节点数据结构

诸如列表和树这样的数据结构,在它们的定义中使用了一种叫节点的递归结构体类型,节点包含一个某种类型的数据字段。现在可以使用空接口作为数据字段的类型,这样我们就能写出通用的代码。下面是实现一个二叉树的部分代码:通用定义、用于创建空节点的 NewNode 方法,及设置数据的 SetData 方法。

  1. package main
  2. import "fmt"
  3. type Node struct {
  4. le *Node
  5. data interface{}
  6. ri *Node
  7. }
  8. func NewNode(left, right *Node) *Node {
  9. return &Node{left, nil, right}
  10. }
  11. func (n *Node) SetData(data interface{}) {
  12. n.data = data
  13. }
  14. func main() {
  15. root := NewNode(nil, nil)
  16. root.SetData("root node")
  17. // make child (leaf) nodes:
  18. a := NewNode(nil, nil)
  19. a.SetData("left node")
  20. b := NewNode(nil, nil)
  21. b.SetData("right node")
  22. root.le = a
  23. root.ri = b
  24. fmt.Printf("%v\n", root) // Output: &{0x125275f0 root node 0x125275e0}
  25. }

接口到接口

一个接口的值可以赋值给另一个接口变量,只要底层类型实现了必要的方法。这个转换是在运行时进行检查的,转换失败会导致一个运行时错误:这是 Go 语言动态的一面,可以拿它和 Ruby 和 Python 这些动态语言相比较。

假定:

  1. var ai AbsInterface // declares method Abs()
  2. type SqrInterface interface {
  3. Sqr() float
  4. }
  5. var si SqrInterface
  6. pp := new(Point) // say *Point implements Abs, Sqr
  7. var empty interface{}

那么下面的语句和类型断言是合法的:

  1. empty = pp // everything satisfies empty
  2. ai = empty.(AbsInterface) // underlying value pp implements Abs()
  3. // (runtime failure otherwise)
  4. si = ai.(SqrInterface) // *Point has Sqr() even though AbsInterface doesn’t
  5. empty = si // *Point implements empty set
  6. // Note: statically checkable so type assertion not necessary.

下面是函数调用的一个例子:

  1. type myPrintInterface interface {
  2. print()
  3. }
  4. func f3(x myInterface) {
  5. x.(myPrintInterface).print() // type assertion to myPrintInterface
  6. }

x 转换为 myPrintInterface 类型是完全动态的:只要 x 的底层类型(动态类型)定义了 print 方法这个调用就可以正常运行

参考

  • Go入门指南
  • Go语言设计与实现

相关文章