Reflect, reading argument and return type from func

Showing 1-3 of 3 messages
Reflect, reading argument and return type from func uki 8/14/11 6:27 AM
Hi,

Is it possible to read a function/method parameter type, and its return type?
I've read the docs http://golang.org/pkg/reflect/ but couldn't find
any fuction for this.

Cheers,
Łukasz Gruner

Re: [go-nuts] Reflect, reading argument and return type from func Sebastien Binet 8/14/11 7:42 AM
Łukasz,

On Sun, 14 Aug 2011 15:27:08 +0200, uki <uka...@gmail.com> wrote:
> Hi,
>
> Is it possible to read a function/method parameter type, and its return type?
> I've read the docs http://golang.org/pkg/reflect/ but couldn't find
> any fuction for this.

once you have the Type of the method or function in hand, you can then
call on of those:

    // In returns the type of a function type's i'th input parameter.
    // It panics if the type's Kind is not Func.
    // It panics if i is not in the range [0, NumIn()).
    In(i int) Type

    // NumIn returns a function type's input parameter count.
    // It panics if the type's Kind is not Func.
    NumIn() int

    // NumOut returns a function type's output parameter count.
    // It panics if the type's Kind is not Func.
    NumOut() int

    // Out returns the type of a function type's i'th output parameter.
    // It panics if the type's Kind is not Func.
    // It panics if i is not in the range [0, NumOut()).
    Out(i int) Type

hth,
-s

Re: Reflect, reading argument and return type from func Arne Vansteenkiste 8/14/11 7:38 AM
Hi, you're probably looking for these:

t := reflect.TypeOf(MyFunction)
t.NumIn() // number of input variables
t.NumOut()
t.In(i) // type of input variable i
t.Out(i)

-Arne.