“所有已知实现”接口的文档

huangapple go评论72阅读模式
英文:

Documentation On "All Known Implementation" of Interfaces

问题

学习Go语言几个月后,我刚刚发现os.File通过实现Read(b []byte) (n int, err error)函数来实现了io.Reader接口。这使得我可以使用缓冲读取器来读取文件,像这样做:

f, err := os.Open("myfile.txt")
bufReader := bufio.NewReader(f)

除非我错过了,Go文档中似乎没有像Java接口文档中那样的“所有已知实现类”的内容。

在Go中有没有办法识别实现了某个接口的类型?

英文:

Few months into learning Go, I just discover that os.File implements the io.Reader interface by implementing the Read(b []byte) (n int, err error) function. This allows me to use a buffered reader to read a file by do something like:

f, err := os.Open("myfile.txt")
bufReader := bufio.NewReader(f)

Unless I miss it, it looks like there isn't an "All Known Implementing Classes" in Go documents on interfaces, like those found in Java interfaces documentation.

Are there any ways to identify the types that implement an interface in Go?

答案1

得分: 11

你可以使用godoc命令的静态分析工具来查找你想要的信息以及更多内容。在命令行中运行以下命令:godoc -http=":8080" -analysis="type"。使用文档可以找出哪些类型实现了一个接口以及一个类型的方法集。

还有一个指针分析工具,可以找到各种类型的调用者和被调用者。通道的发送<---接收分析非常方便。

你还可以在http://golang.org/lib/godoc/analysis/help.html上阅读有关godoc工具进行的静态分析的更多信息。

英文:

You can find the info you want and more using the godoc command's static analysis tools. Run the following at the command line: godoc -http=&quot;:8080&quot; -analysis=&quot;type&quot;. Using the documentation you can find out what types implement an interface and the method set for a type.

There is also a pointer analysis that allows you to find callers and callees of various types. The channel send<--->receive analysis is pretty neat.

You can also read more about the static analysis done by the godoc tool at http://golang.org/lib/godoc/analysis/help.html

答案2

得分: 4

你可以使用https://github.com/dominikh/implements来实现这个功能:

> implements是一个命令行工具,可以告诉你哪些类型实现了哪些接口,或者哪些接口被哪些类型实现。

例如:

~ implements -types=crypto/cipher
crypto/cipher.StreamReader 实现了...
        io.Reader
*crypto/cipher.StreamReader 实现了...
        io.Reader
crypto/cipher.StreamWriter 实现了...
        io.Closer
        io.WriteCloser
        io.Writer
*crypto/cipher.StreamWriter 实现了...
        io.Closer
        io.WriteCloser
        io.Writer
英文:

https://github.com/dominikh/implements can do this:

> implements is a command line tool that will tell you which types implement which interfaces, or which interfaces are implemented by which types.

e.g.

~ implements -types=crypto/cipher
crypto/cipher.StreamReader implements...
        io.Reader
*crypto/cipher.StreamReader implements...
        io.Reader
crypto/cipher.StreamWriter implements...
        io.Closer
        io.WriteCloser
        io.Writer
*crypto/cipher.StreamWriter implements...
        io.Closer
        io.WriteCloser
        io.Writer

答案3

得分: 3

对于所有的vim迷们,vim-go支持使用:GoImplements:GoCallees:GoChannelPeers:GoReferrers等oracle命令进行高级代码分析。

例如,如果我有一个看起来像这样的Calculator接口和实现:

type Arithmetic interface{
  add(float64, float64) float64 
}

type Calculator struct{}

func (c *calculator) add(o1, o2 float64) float64 {
  // ... stuff
}

然后在vim中运行:GoImplements,光标放在type Arithmetic interface上,会得到类似以下的结果:

calculator.go|8 col 6| 接口类型 Arithmetic
calculator.go|3 col 6| 被指针类型 *calculator 实现

现在,如果我将光标移动到type Calculator struct{}行并运行:GoImplements,会得到类似以下的结果:

calculator.go|3 col 6| 指针类型 *calculator
calculator.go|8 col 6| 实现了 Arithmetic

注意:如果你遇到了"unknown command"错误,请尝试在重新尝试之前先执行:GoInstallBinaries命令。

英文:

For all you vim junkies out there, vim-go supports advance code analysis using the :GoImplements, :GoCallees, :GoChannelPeers, :GoReferrers etc. oracle commands.

For example, if I have a Calculator interface and implementation that looks like:

type Arithmetic interface{
  add(float64, float64) float64 
}

type Calculator struct{}

func (c *calculator) add(o1, o2 float64) float64 {
  // ... stuff
}

Then running :GoImplements in vim with my cursor on the type Arithmetic interface will yield something like:

calculator.go|8 col 6| interface type Arithmetic
calculator.go|3 col 6| is implemented by pointer type *calculator

Now if I moved my cursor to the type Calculator struct{} line and run :GoImplements, I will get something like:

calculator.go|3 col 6| pointer type *calculator
calculator.go|8 col 6| implements Arithmetic

Note: If you got an "unknown command" error, try execute :GoInstallBinaries first before re-trying.

huangapple
  • 本文由 发表于 2015年8月1日 14:53:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/31759184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定