Created
December 1, 2022 04:38
-
-
Save IamFaizanKhalid/a7cead62294f1b5edf885a438d6ad00e to your computer and use it in GitHub Desktop.
Function to get current function's name in Golang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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