This program prints the functions taking an int
as parameter or returning an int
:
package main
import (
"fmt"
"reflect"
)
func main() {
funcs := make([]interface{}, 3, 3) // I use interface{} to allow any kind of func
funcs[0] = func (a int) (int) { return a+1} // good
funcs[1] = func (a string) (int) { return len(a)} // good
funcs[2] = func (a string) (string) { return ":("} // bad
for _, fi := range funcs {
f := reflect.ValueOf(fi)
functype := f.Type()
good := false
for i:=0; i<functype.NumIn(); i++ {
if "int"==functype.In(i).String() {
good = true // yes, there is an int among inputs
break
}
}
for i:=0; i<functype.NumOut(); i++ {
if "int"==functype.Out(i).String() {
good = true // yes, there is an int among outputs
break
}
}
if good {
fmt.Println(f)
}
}
}
I think the code is self explanatory
type Node struct { Neighbors []*Node; Fn func(int...) int}
, or using a recursive interface liketype Node interface { Children []Node; Fn(int...) int }
. There's probably a way to avoid reflect that will save you some trouble. Rob Pike's talk "Lexical Scanning in Go" discusses the recursive interface thing. It's very useful. – Calciminetype Node struct { F interface{} }
and stick functions in it, you can make this a little easier to maintain: play.golang.org/p/ANJBr1i8Zh. Cool problem. – Calcimine