Get name of variable with function literal

2,641 views
Skip to first unread message

Archos

unread,
Dec 3, 2014, 7:50:11 AM12/3/14
to golan...@googlegroups.com
Is there any way to get the variable name which has assigned a function literal?

I've played with reflect but I think that it is not possible:

http://play.golang.org/p/Ts-FzCb_mF

egon

unread,
Dec 3, 2014, 8:31:37 AM12/3/14
to golan...@googlegroups.com
Any particular reason you want to do this?

Lars Seipel

unread,
Dec 3, 2014, 8:35:48 AM12/3/14
to Archos, golan...@googlegroups.com
On Wed, Dec 03, 2014 at 04:50:11AM -0800, Archos wrote:
> I've played with reflect but I think that it is not possible:

It's not. At runtime, that name doesn't exist anymore.

Archos

unread,
Dec 3, 2014, 9:05:11 AM12/3/14
to golan...@googlegroups.com
From a file file generator, I have a function whose argument is a type of function.

So, at generating the new file, I need to know the name of that type of function for that it can be called from the file generated.

egon

unread,
Dec 3, 2014, 10:04:23 AM12/3/14
to golan...@googlegroups.com
On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:
From a file file generator, I have a function whose argument is a type of function.

So, at generating the new file, I need to know the name of that type of function for that it can be called from the file generated.

Simply a struct

Or wrap/register the funcs with the name 

Of course there's a hackish way... but every time you use it, a kitten dies:

(Not sure whether you meant the function name that's stored in the variable name... or actually the variable name - you can't get the variable name.) 

+ Egon

Archos

unread,
Dec 3, 2014, 11:25:23 AM12/3/14
to golan...@googlegroups.com
Finally, I implemented using a struct/interface, Thanks for the examples.


On Wednesday, December 3, 2014 3:04:23 PM UTC, egon wrote:
On Wednesday, 3 December 2014 16:05:11 UTC+2, Archos wrote:
From a file file generator, I have a function whose argument is a type of function.

So, at generating the new file, I need to know the name of that type of function for that it can be called from the file generated.

Simply a struct

Or wrap/register the funcs with the name 

Of course there's a hackish way... but every time you use it, a kitten dies:

(Not sure whether you meant the function name that's stored in the variable name... or actually the variable name - you can't get the variable name.) 
I mean the variable name which calls to the closure.
 

Dmitri Shuralyov

unread,
Dec 4, 2014, 12:09:11 AM12/4/14
to golan...@googlegroups.com
If you have access to the source code in its original location when running code, you could use this code get the variable name. It uses runtime stack trace to figure out the path and line number of the caller, then parses the original source AST to find out the variable name. It won't work if the source code is missing or moved to a different location.

http://godoc.org/github.com/shurcooL/go/gists/gist6418290#GetExprAsString (better package name suggestions are welcome, I'm not good with coming up with names)

f := func(n int) int {
return n + 1
}
fmt.Println("Name of func:", gist6418290.GetExprAsString(f))

// Output:
//f

Now, it works in practice and was a fun challenge to implement, but I don't recommend you actually use it... just because I'm sure there's a simpler solution for your particular problem (it looks like you already found it).

Dmitri Shuralyov

unread,
Dec 4, 2014, 12:11:48 AM12/4/14
to golan...@googlegroups.com
The output should've been:

// Output:
//Name of func: f

Sorry, this is so frustrating that there's no way to edit a post I _just_ made and I'm so used to it everywhere (Gmail, GitHub, HN, reddit, etc.). :(
Reply all
Reply to author
Forward
0 new messages