英文:
How to find out which types implement which interface
问题
在io包中,类型ByteReader定义了一个接口,该接口包装了方法ReadByte() (c byte, err error)。
最简单的方法是找出标准库中(即golang.org/pkg中列出的)满足该接口的类型是什么。
这只是经验问题,还是有其他帮助的?
英文:
Example:
In package io the type ByteReader defines an interface that wraps the method ReadByte() (c byte, err error).
What is the easiest way to find out which types in the standard library (i.e. listed here in golang.org/pkg) satisfy this interface?
Is this just a matter of experience or is there any other help?
答案1
得分: 19
主要是通过经验。无论如何,举个例子:
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 网站还具有区分大小写的搜索功能。
英文:
Mostly by experience. Anyway, for example:
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$
And also the golang.org site has a case sensitive search capability.
答案2
得分: 11
现在有比简单搜索更好的方法来做这个。
Go Oracle有一个implements query,可以显示哪些类型实现了特定的接口,以及哪些接口被特定类型实现。
此外,这里有一个声称提供相同功能的工具:https://github.com/dominikh/implements。
2020年更新:官方的Go语言服务器gopls也支持这个查询:
➜ gopls implementation -h
显示所选标识符的实现
用法:implementation [flags] <position>
示例:
$ # 目标标识符的1索引位置(:line:column或:#offset)
$ gopls implementation helper/helper.go:8:6
$ gopls implementation helper/helper.go:#53
英文:
There are now better ways to do this than simply searching.
Go Oracle has an implements query that will show which types implement a particular interface, and which interfaces a particular type implements.
Additionally, here is a tool that claims to offer the same functionality: https://github.com/dominikh/implements.
2020 Update: The official Go language server, gopls, also has support for this query:
➜ 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
答案3
得分: 1
使用源代码浏览器:https://cs.opensource.google/go/go/。
在文档中(例如https://pkg.go.dev/io#ByteReader),点击类型的名称:
它会带您进入源代码:
现在,再次点击方法名称,它将打开底部的“References”部分:
在我看来,这不是非常方便,但比命令行工具要好。这种信息应该直接从文档中获取!
英文:
Use the source code browser: https://cs.opensource.google/go/go/ .
When in the docs (e.g. https://pkg.go.dev/io#ByteReader ) click on the name of the type:
It will take you to the source code:
Now, click again on the method name, it will open the "References" deck on the bottom:
In my opinion it's not super convenient, but better than cli tools. This kind of information should be available straight from the docs!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论