can i get the function name from reflect.Value?

2,853 views
Skip to first unread message

woo

unread,
Nov 17, 2010, 8:18:46 AM11/17/10
to golang-nuts
hi:
i know we can use "Get" to judge the "i" and the test interface.

func test(){
}

func ftestInterface(i interface{}){
if reflect.NewValue(i).(*reflect.FuncValue).Get() == reflect.NewValue(test).(*reflect.FuctValue).Get()){
fmt.Printf("equal\n")
}
}

But is there any way i can get the function from test ?ie:
func getFuncName(i interface{}){
return "test"
}

Andrew Gerrand

unread,
Nov 18, 2010, 5:21:11 PM11/18/10
to woo, golang-nuts
Unfortunately not. A function name is like a variable name; the
reflect package can see type names, but it doesn't know about variable
or function names.

Reflect can, however, see the names of methods. Maybe there's a
workaround you can apply there, depending on your application.

Andrew

gaxxx

unread,
Nov 19, 2010, 12:00:42 AM11/19/10
to Andrew Gerrand, golang-nuts
thanks for your reply.
yes, the method name is like  "func ()"
maybe the only way to work out is that put the function as  "member function" of a struct,and get the method name.


2010/11/19 Andrew Gerrand <a...@golang.org>



--
woo
gtalk: woos...@gmail.com
15801207407
Reply all
Reply to author
Forward
0 new messages