Possible to get the names of function parameters?

61 views
Skip to first unread message

robfig

unread,
Nov 11, 2011, 1:23:31 PM11/11/11
to golan...@googlegroups.com
Hello,

I see that it's possible to get the types of parameters to a function using the reflect module.  Is there any way (even a very hacky way) of getting the names of the parameters as well?  

The goal is to automatically parse form parameters and call the handler function with them.  e.g. 
<input name="id" value="5">
<input name="name" value="foo">

func handler(id int, name string) { ... }

Thanks,
Rob

Ian Lance Taylor

unread,
Nov 11, 2011, 1:46:01 PM11/11/11
to golan...@googlegroups.com
robfig <rob...@gmail.com> writes:

> I see that it's possible to get the types of parameters to a function using
> the reflect module. Is there any way (even a very hacky way) of getting
> the names of the parameters as well?

No.


> The goal is to automatically parse form parameters and call the handler
> function with them. e.g.
> <input name="id" value="5">
> <input name="name" value="foo">
>
> func handler(id int, name string) { ... }

You could have the function take a single value of struct type.
Reflection would give you the field names.

Ian

chrisfarms

unread,
Nov 11, 2011, 5:42:05 PM11/11/11
to golan...@googlegroups.com

> The goal is to automatically parse form parameters and call the handler
> function with them.  e.g. 

You could have the function take a single value of struct type.


Reflection would give you the field names.

This would def be the best way to approach this. 

You might find the UnmarshalForm function from goweb useful for reading the form values into a struct:

robfig

unread,
Nov 11, 2011, 11:48:16 PM11/11/11
to golan...@googlegroups.com
Thank you both
Reply all
Reply to author
Forward
0 new messages