> For the complete documentation index, see [llms.txt](https://go.xiao5.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.xiao5.info/di-si-bu-fen-shi-ji-ying-yong/16.0/16.6.md).

# 使用指针指向接口类型

查看如下程序：`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](https://github.com/MartialBE/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_16/pointer_interface.go) （不能通过编译）:

```go
package main
import (
    "fmt"
)
type nexter interface {
    next() byte
}
func nextFew1(n nexter, num int) []byte {
    var b []byte
    for i:=0; i < num; i++ {
        b[i] = n.next()
    }
    return b
}
func nextFew2(n *nexter, num int) []byte {
    var b []byte
    for i:=0; i < num; i++ {
        b[i] = n.next() // 编译错误：n.next 未定义（*nexter 类型没有 next 成员或 next 方法）
    }
    return b
}
func main() {
    fmt.Println("Hello World!")
}
```

**永远不要使用一个指针指向一个接口类型，因为它已经是一个指针。**

## 链接

* [目录](https://github.com/MartialBE/the-way-to-go_ZH_CN/blob/master/eBook/directory.md)
* 上一节：[不需要将一个指向切片的指针传递给函数](/di-si-bu-fen-shi-ji-ying-yong/16.0/16.5.md)
* 下一节：[使用值类型时误用指针](/di-si-bu-fen-shi-ji-ying-yong/16.0/16.7.md)
