How to find out which types implement which interface

前端 未结 2 830
南笙
南笙 2020-12-29 06:46

Example:

In package io the type ByteReader defines an interface that wraps the method ReadByte() (c byte, err error).

What is the

相关标签:
2条回答
  • 2020-12-29 07:00

    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.

    0 讨论(0)
  • 2020-12-29 07:09

    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
    
    0 讨论(0)
提交回复
热议问题