131

Given a function, is it possible to get its name? Say:

func foo() {
}

func GetFunctionName(i interface{}) string {
    // ...
}

func main() {
    // Will print "name: foo"
    fmt.Println("name:", GetFunctionName(foo))
}

I was told that runtime.FuncForPC would help, but I failed to understand how to use it.

4 Answers 4

248
+500

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))
}
4
  • 4
    While this seems to work, some care may be required here: the documentation for .Pointer() states "If v's Kind is Func, the returned pointer is an underlying code pointer, but not necessarily enough to identify a single function uniquely. The only guarantee is that the result is zero if and only if v is a nil func Value."
    – jochen
    Apr 14, 2015 at 15:56
  • 1
    @jochen does "not a single func" means that it could return false positives (i.e. the pointer of a different function) ?
    – themihai
    Feb 1, 2016 at 0:46
  • 1
    @themihai I don't know, the sentence I quoted is all the docs at golang.org/pkg/reflect/#Value.Pointer say about this. But the quote seems to indicate that one could get the same pointer for different functions, doesn't it? And if this is the case, GetFunctionName might return the same name for different functions?
    – jochen
    Feb 2, 2016 at 21:10
  • 5
    @jochen I think this has to do with closures; if you create two closures that have the same underlying function, they will be equivalent even if the values they close over are different. Jun 8, 2016 at 20:29
15

Not exactly what you want, because it logs the filename and the line number, but here is how I do it in my Tideland Common Go Library (http://tideland-cgl.googlecode.com/) using the "runtime" package:

// Debug prints a debug information to the log with file and line.
func Debug(format string, a ...interface{}) {
    _, file, line, _ := runtime.Caller(1)
    info := fmt.Sprintf(format, a...)

    log.Printf("[cgl] debug %s:%d %v", file, line, info)
1
  • 5
    That doesn't really help. I don't need to get information about the call stack, but about a given function. I believe the question would be answered if I knew how to get the corresponding pc given a function reference (if it is possible).
    – moraes
    Aug 13, 2011 at 23:01
4

I found a better solution, in this function down here you just simply pass a function and the output is gonna be simple and straight.

package main

import (
    "reflect"
    "runtime"
    "strings"
)

func GetFunctionName(temp interface{}) string {
    strs := strings.Split((runtime.FuncForPC(reflect.ValueOf(temp).Pointer()).Name()), ".")
    return strs[len(strs)-1]
}

And this is an example of how you use this:

package main

import "fmt"

func main() {
    fmt.Println(GetFunctionName(main))
}

And this is the answer you should expect:

main
4
  • 1
    What's the point of this? You're literally just returning the same string you passed to it. May as well just do fmt.Println("main")
    – Gostega
    Apr 21, 2022 at 4:33
  • 3
    @Gostega I passed a function, not a string... This solution is actually what the question wanted. Read the question again.
    – Mahmood
    Apr 21, 2022 at 10:21
  • you're right, the question does ask that, I apologise. I think I read between the lines a bit on the question because it made no sense to me as written. It seems completely pointless to get the name of a function by passing the function's name. If one already has the name, just print it or use it, why pass that name to another function which simply returns the same name back.
    – Gostega
    Apr 21, 2022 at 23:48
  • The point, at least why I'm searching for the answer, is that you could write a library that expects users to pass in a function and for feedback use the name of the function to do something. For instance, a test framework that would use the name for identifying the test name. Sep 9, 2022 at 10:14
3

By getting the function name of the previous caller:

import (
    "os"
    "runtime"
)

func currentFunction() string {
    counter, _, _, success := runtime.Caller(1)

    if !success {
        println("functionName: runtime.Caller: failed")
        os.Exit(1)
    }

    return runtime.FuncForPC(counter).Name()
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.