【问题标题】:How to find out which types implement which interface如何找出哪些类型实现了哪个接口
【发布时间】:2013-01-29 06:47:23
【问题描述】:

例子:

在包io 中,ByteReader 类型定义了一个封装方法ReadByte() (c byte, err error) 的接口。

找出 哪些类型在标准库(即listed here in golang.org/pkg满足此接口的最简单方法是什么?

这只是经验问题还是有其他帮助?

【问题讨论】:

    标签: go


    【解决方案1】:

    主要是凭经验。无论如何,例如:

    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

    【讨论】:

      【解决方案2】:

      现在有比简单搜索更好的方法来做到这一点。

      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
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2011-11-15
      • 2011-10-26
      • 1970-01-01
      • 2012-01-20
      相关资源
      最近更新 更多