Is it possible to obtain a variable name at runtime?

C has macros, and in a macro the pound sign (#) in front of a parameter name produced a string that contained the variable name. Handy for debugging. Okay, it’s more like compile time than runtime. I don’t suppose there is any way to do this in Go, is there?

C example:
#define debug§ printf( #p “: %d\n”, p)

This is not possible.

If the field is part of a struct, you can recover the name via reflection. fmt’s %#v verb is useful here.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.