【发布时间】:2013-01-29 06:47:23
【问题描述】:
例子:
在包io 中,ByteReader 类型定义了一个封装方法ReadByte() (c byte, err error) 的接口。
找出 哪些类型在标准库(即listed here in golang.org/pkg)满足此接口的最简单方法是什么?
这只是经验问题还是有其他帮助?
【问题讨论】:
标签: go
例子:
在包io 中,ByteReader 类型定义了一个封装方法ReadByte() (c byte, err error) 的接口。
找出 哪些类型在标准库(即listed here in golang.org/pkg)满足此接口的最简单方法是什么?
这只是经验问题还是有其他帮助?
【问题讨论】:
标签: go
主要是凭经验。无论如何,例如:
jnml@fsc-r630:~/go/src/pkg$ egrep -nr '^func (.*) ReadByte\(' *
bufio/bufio.go:165:func (b *Reader) ReadByte() (c byte, err error) {
bytes/reader.go:59:func (r *Reader) ReadByte() (b byte, err error) {
bytes/buffer.go:289:func (b *Buffer) ReadByte() (c byte, err error) {
encoding/xml/xml_test.go:234:func (d *downCaser) ReadByte() (c byte, err error) {
strings/reader.go:58:func (r *Reader) ReadByte() (b byte, err error) {
jnml@fsc-r630:~/go/src/pkg$
golang.org 网站也有一个case sensitive search capability。
【讨论】:
现在有比简单搜索更好的方法来做到这一点。
Go Oracle 有一个implements query,它将显示哪些类型实现了特定接口,以及特定类型实现了哪些接口。
此外,这里有一个声称提供相同功能的工具:https://github.com/dominikh/implements。
2020 年更新:官方 Go 语言服务器 gopls 也支持此查询:
➜ gopls implementation -h
display selected identifier's implementation
Usage: implementation [flags] <position>
Example:
$ # 1-indexed location (:line:column or :#offset) of the target identifier
$ gopls implementation helper/helper.go:8:6
$ gopls implementation helper/helper.go:#53
【讨论】:
encoding 包中,pkg.go.dev 网站有一个可以查看的 importby 选项卡 (pkg.go.dev/encoding?tab=importedby) 您可以在那里找到实现的可能性很高。