寻找返回特定类型的函数 [英] Finding functions that return a specific type

查看:91
本文介绍了寻找返回特定类型的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

例如有很多以io.Writer作为参数的函数。现在我想知道如何创建一个io.Writer,并且有很多方法可以这样做。但是,我怎样才能轻松找到所有的方法,而不用猜测包,并查找所有的方法来找到那些返回io.Writer(或任何其他类型)?

编辑:我应该扩展我的问题以找到实现特定接口的类型。坚持使用io.Writer示例(这确实是原始问题的一个不好的例子),找到实现Writer接口的任何类型是很好的方法,因为这些类型对于需要使用的函数来说是有效的参数一个io.Writer,因此在类型为接口时回答原始问题。

解决方案

很少需要找到返回 Int16 错误的函数(func可以在Go中返回一些值,你知道的) p>

对于问题的第二部分,存在由Dominik Honnef 执行的精彩cmd 实现 go get honnef .co / go / implements
在发现满足条件的类型后,您可以假设类型的构造函数(类似于 func NewTheType()TheType )将在源代码和文档中的TheType声明之后。这是一个经过验证的Go实践。


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.

解决方案

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.

这篇关于寻找返回特定类型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆