学习Go几个月后,我发现os.File
通过实现Read(b []byte) (n int, err error)
函数实现了Read(b []byte) (n int, err error)
接口。这允许我使用缓冲的读取器通过执行以下操作来读取文件:
f, err := os.Open("myfile.txt")
bufReader := bufio.NewReader(f)
除非我错过了,否则界面上的Go文档中似乎没有“所有已知的实现类”,就像Java接口文档中的那些类一样。
是否有任何方法来识别Go中实现接口的类型?
发布于 2015-07-31 23:14:52
您可以使用godoc命令的静态分析工具找到您想要的信息和更多信息。在命令行中运行以下命令:godoc -http=":8080" -analysis="type"
。使用文档,您可以了解实现接口的类型和为类型设置的方法。
还有一个指针分析,允许您找到不同类型的调用者和调用者。频道发送<
您还可以在http://golang.org/lib/godoc/analysis/help.html上阅读更多关于godoc工具所做的静态分析的内容。
发布于 2015-07-31 23:13:58
https://github.com/dominikh/implements可以这样做:
implements是一个命令行工具,它将告诉您哪些类型实现了哪些接口,哪些接口是由哪种类型实现的。
例如:
~ 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
发布于 2015-08-05 20:32:20
对于所有的病毒瘾君子来说,维姆-去支持使用: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| interface type Arithmetic
calculator.go|3 col 6| is implemented by pointer type *calculator
现在,如果我将光标移到type Calculator struct{}
行并运行:GoImplements
,我将得到如下内容:
calculator.go|3 col 6| pointer type *calculator
calculator.go|8 col 6| implements Arithmetic
注意:如果您有“未知命令”错误,请在重试之前先执行:GoInstallBinaries
。
https://stackoverflow.com/questions/31759184
复制相似问题