如何在Go语言中找到实现某个接口的类型

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

How to find types which implement an interface in go

问题

我需要一个 io.Writer 用于一个函数。我不知道如何从文件中获取一个...

我知道接口是隐式的,所以这让搜索变得复杂...

英文:

I need an io.Writer for a function. I don't know how to get one from a file...

I know interface are implicit so it complicated the search...

答案1

得分: 5

请看一下os.File的文档:它有一个func (*File) Write方法,这意味着它是一个写入器。

你可以使用命令guru来列出实现某个接口的所有类型。特别是**implements查询**:

> implements查询显示被选类型实现的接口,如果被选类型本身是一个接口,则显示实现它的具体类型集合。
>
> 对于值的implements查询会报告关于表达式类型的相同信息。
>
> 对于方法的implements查询会显示与该方法相关的抽象或具体方法的集合。

英文:

Look at the os.File documentation: it has a func (*File) Write method, which means it is a Writer.

You can use the command guru to list all types implementing an interface.
Notably, the implements query:

> The implements query shows interfaces that are implemented by the selected type and, if the selected type is itself an interface, the set of concrete types that implement it.
>
> An implements query on a value reports the same information about the expression’s type.
>
> An implements query on a method shows the set of abstract or concrete methods that are related to it

huangapple
  • 本文由 发表于 2017年1月13日 03:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/41621525.html
匿名

发表评论

匿名网友

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

确定