On 2013/2/21 Joshua Poehls <
jos...@poehls.me> wrote:
> Similar, but I was actually referring to the approximate type signature
> search that Hoogle offers.
>
> Here are some example queries with syntax slightly tweaked from Hoogle.
>
> "_a -> _a" to find any func that takes 1 argument and returns 1 value with
> the same type as the argument. This would match the math Abs func: func
> Abs(x float64) float64
>
> "float64 -> float64" to find any func that takes 1 float64 arg and returns 1
> float64 value. Also matches math Abs func.
>
> "([Writer], string) -> (int, error)" to find any func that takes 2 args, the
> first being any type that implements the Writer interface, the 2nd being a
> string, and returns a tuple of an int and error. This would find strings
> func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)
>
> "(_a, _b) -> (int, error)" to find any func that takes 2 args of different
> types (any types) and returns a tuple of int and error. Would also match
> WriteString.
>
> Barring insanely crafty regex searches, I don't believe it's currently
> possible to do these types of searches over Go packages. I'm mostly
> wondering if this is something people would find useful.
>
> I think Hoogle is insanely useful for Haskell but that could be due to the
> nature of the Haskell language.
I took some time to write such a tool that searches in the Go API file
(so it works for the standard library or anything you run through the
api tool).
To fetch it:
go get
github.com/remyoudompheng/go-misc/apisearch
By default it looks into the Go 1 API file. Here are some examples:
$ ./apisearch -e "func ([]a, a) int"
using pattern: func([]a, a) int
looking in "/opt/remy/go/api/go1.txt"
bytes.IndexByte: func ([]uint8, uint8) int
sort.SearchFloat64s: func ([]float64, float64) int
sort.SearchInts: func ([]int, int) int
sort.SearchStrings: func ([]string, string) int
$ ./apisearch -e "func(io.Reader) a"
using pattern: func(io.Reader) a
looking in "/opt/remy/go/api/go1.txt"
archive/tar.NewReader: func (io.Reader) *Reader
bufio.NewReader: func (io.Reader) *Reader
compress/bzip2.NewReader: func (io.Reader) io.Reader
compress/flate.NewReader: func (io.Reader) io.ReadCloser
encoding/ascii85.NewDecoder: func (io.Reader) io.Reader
encoding/csv.NewReader: func (io.Reader) *Reader
encoding/gob.NewDecoder: func (io.Reader) *Decoder
encoding/json.NewDecoder: func (io.Reader) *Decoder
encoding/xml.NewDecoder: func (io.Reader) *Decoder
io/ioutil.NopCloser: func (io.Reader) io.ReadCloser
net/http/httputil.NewChunkedReader: func (io.Reader) io.Reader
testing/iotest.DataErrReader: func (io.Reader) io.Reader
testing/iotest.HalfReader: func (io.Reader) io.Reader
testing/iotest.OneByteReader: func (io.Reader) io.Reader
testing/iotest.TimeoutReader: func (io.Reader) io.Reader
$ ./apisearch -e "func (...a) a"
using pattern: func(...a) a
looking in "/opt/remy/go/api/go1.txt"
io.MultiReader: func (...Reader) Reader
io.MultiWriter: func (...Writer) Writer
path.Join: func (...string) string
path/filepath.Join: func (...string) string
$ ./apisearch -e "func(string, ...interface{}) a"
using pattern: func(string, ...interface{}) a
looking in "/opt/remy/go/api/go1.txt"
fmt.Errorf: func (string, ...interface{}) error
fmt.Sprintf: func (string, ...interface{}) string
A few corner cases are not implemented, have fun!
Rémy.