Skip to content

Instantly share code, notes, and snippets.

@IamFaizanKhalid
Created December 1, 2022 04:38
Show Gist options
  • Save IamFaizanKhalid/a7cead62294f1b5edf885a438d6ad00e to your computer and use it in GitHub Desktop.
Save IamFaizanKhalid/a7cead62294f1b5edf885a438d6ad00e to your computer and use it in GitHub Desktop.
Function to get current function's name in Golang.
package main
import (
"fmt"
"runtime"
)
func main() {
myFunction()
}
func myFunction() {
fmt.Println(GetFunctionName())
}
func GetFunctionName() string {
x, _, _, _ := runtime.Caller(1)
return fmt.Sprintf("%s()", runtime.FuncForPC(x).Name())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment