How to get the name of a function in Go?

I found a solution: package main import ( “fmt” “reflect” “runtime” ) func foo() { } func GetFunctionName(i interface{}) string { return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() } func main() { // This will print “name: main.foo” fmt.Println(“name:”, GetFunctionName(foo)) }

Access struct property by name

Most code shouldn’t need this sort of dynamic lookup. It’s inefficient compared to direct access (the compiler knows the offset of the X field in a Vertex structure, it can compile v.X to a single machine instruction, whereas a dynamic lookup will need some sort of hash table implementation or similar). It’s also inhibits static … Read more