第十六章 (#846)

Co-authored-by: Joe Chen <jc@unknwon.io>
This commit is contained in:
Haigang Zhou
2022-05-17 16:36:50 +08:00
committed by GitHub
parent 72f2eccbc5
commit 72bc74ab95
10 changed files with 38 additions and 39 deletions

View File

@@ -1,9 +1,8 @@
# 16.6 使用指针指向接口类型
查看如下程序:`nexter` 是一个接口类型,并且定义了一个 `next()` 方法读取下一字节。函数 `nextFew1``nexter` 接口作为参数并读取接下来的 `num` 个字节,并返回一个切片:这是正确做法。但是 `nextFew2` 使用一个指向 `nexter` 接口类型的指针作为参数传递给函数:当使用 `next()` 函数时,系统会给出一个编译错误:**n.next undefined (type *nexter has no
field or method next)** 译者注n.next 未定义(*nexter 类型没有 next 成员或 next 方法))
查看如下程序:`nexter` 是一个接口类型,并且定义了一个 `next()` 方法读取下一字节。函数 `nextFew1``nexter` 接口作为参数并读取接下来的 `num` 个字节,并返回一个切片:这是正确做法。但是 `nextFew2` 使用一个指向 `nexter` 接口类型的指针作为参数传递给函数:当使用 `next()` 函数时,系统会给出一个编译错误:**`n.next undefined (type *nexter has no field or method next)`** 译者注n.next 未定义(*nexter 类型没有 next 成员或 next 方法))
例 16.1 [pointer_interface.go](examples/chapter_16/pointer_interface.go) (不能通过编译):
例 16.1 [pointer_interface.go](examples/chapter_16/pointer_interface.go) 不能通过编译:
```go
package main