Reflect, reading argument and return type from func

3,266 views
Skip to first unread message

uki

unread,
Aug 14, 2011, 9:27:08 AM8/14/11
to golang-nuts
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

Sebastien Binet

unread,
Aug 14, 2011, 10:42:50 AM8/14/11
to uki, golang-nuts
Ł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

barnex

unread,
Aug 14, 2011, 10:38:12 AM8/14/11
to golang-nuts
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.
Reply all
Reply to author
Forward
0 new messages