查找返回特定类型的函数

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

Finding functions that return a specific type

问题

也许这是一个愚蠢的问题,但是否有一种方法可以找到所有返回特定类型的函数(在标准库或GOPATH中)?

例如,有许多函数将io.Writer作为参数。现在我想知道如何创建一个io.Writer,有很多方法可以做到这一点。但是,如何在不猜测包名并查找所有方法以找到返回io.Writer(或任何其他类型)的方法的情况下轻松找到所有方法?

**编辑:**我应该扩展我的问题,以找到实现特定接口的类型。以io.Writer为例(诚然,这对于原始问题来说是一个糟糕的例子),有一种方法可以找到实现Writer接口的任何类型,因为这些类型将是接受io.Writer作为参数的函数的有效参数,从而回答了原始问题。

英文:

Perhaps this is a silly question, but is there a way to find all functions (in the standard library or GOPATH) that return a specific type?

For example there are many functions that take io.Writer as an argument. Now I want to know how to create an io.Writer and there are many ways to do so. But how can I find all the ways easily without guessing at packages and looking through all the methods to find those that return an io.Writer (or whatever other type I'm after)?

Edit: I should extend my question to also find types that implement a specific interface. Sticking with the io.Writer example (which admittedly was a bad example for the original question) it would be good to have a way to find any types that implement the Writer interface, since those types would be valid arguments for a function that takes takes an io.Writer and thus answer the original question when the type is an interface.

答案1

得分: 3

也许不是最好的方法,但是看一下官方golang.org网站顶部的搜索框。如果你搜索"Writer"

http://golang.org/search?q=Writer

你会得到许多结果,按照以下类别进行分组:

还要注意,io.Writer是一个接口,我们都知道Go如何处理接口:在实现接口时,没有声明意图,如果接口定义的方法被声明,类型会隐式地实现接口。这意味着你可能找不到很多创建并返回io.Writer的示例,因为类型的命名可能完全不同,但仍然是io.Writer

如果你寻找非接口类型,例如bytes.Buffer,事情会变得稍微容易一些。

此外,在类型的声明包的文档中,Index部分按类型对包的函数和方法进行分组,因此你可以在Index部分的条目/链接下方找到返回你所寻找的类型的函数和方法。

还要注意,你可以在godoc.org上检查包的依赖关系。例如,你可以查看导入io包的其他包,这可能是寻找进一步示例的好起点(对于io包来说,这可能会很耗时,因为目前有23410个包导入它)。

英文:

Maybe not the best way but take a look at the search field at the top of the official golang.org website. If you search for "Writer":

http://golang.org/search?q=Writer

You get many results, grouped by categories like

Also note that io.Writer is an interface, and we all know how Go handles interfaces: when implementing an interface, there is no declaration of intent, a type implicitly implements an interface if the methods defined by the interface are declared. This means that you won't be able to find many examples where an io.Writer is created and returned because a type might be named entirely different and still be an io.Writer.

Things get a little easier if you look for a non-interface type for example bytes.Buffer.

Also in the documentation of the declaring package of the type the Index section groups functions and methods of the package by type so you will find functions and methods of the same package that return the type you're looking for right under its entry/link in the Index section.

Also note that you can check the dependencies of packages at godoc.org. For example you can see what packages import the io package which may be a good starting point to look for further examples (this would be exhausting in case of package io because it is so general that at the moment 23410 packages import it).

答案2

得分: 2

在我的编码过程中,我个人很少需要找到返回Int16error的函数(在Go中,函数可以返回多个值,你知道的)。

对于你问题的第二部分,有一个很棒的命令行工具implements,由Dominik Honnef编写,可以通过go get honnef.co/go/implements进行安装。
在找到满足你条件的类型之后,你可以假设该类型的构造函数(类似于func NewTheType() TheType)就在TheType的声明之后的源代码和文档中。这是一种经过验证的Go实践方法。

英文:

In my days coding I'm personally rare in need to find functions returning Int16 and error(func can return few values in Go, you know)

For second part of your question there exists wonderful cmd implements written by Dominik Honnef go get honnef.co/go/implements
After discover type that satisfy your conditions you can assume constructor for type (something like func NewTheType() TheType) would be just after TheType declaration in source code and docs. It's a proven Go practice.

huangapple
  • 本文由 发表于 2015年4月10日 13:59:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/29554369.html
匿名

发表评论

匿名网友

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

确定