Skip to content

Instantly share code, notes, and snippets.

@cyantarek
Forked from salihzain/main.go
Created January 16, 2021 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyantarek/a07fcdb289c43a728d002b6bdf470698 to your computer and use it in GitHub Desktop.
Save cyantarek/a07fcdb289c43a728d002b6bdf470698 to your computer and use it in GitHub Desktop.
Go code to get the name of the function that invoked the current function
package main
import (
"fmt"
"runtime"
)
// whoCalledMe is a function that returns the name, fileName, and lineNumber of the caller that called function X
// the code doesn't check for edge cases
func whoCalledMe() (callerName, callerFileName string, callerLineNumber int) {
// program counters, only interested in 1 pc, the pc of my caller
pc := make([]uintptr, 1)
// skip == 0 is runtime.Callers itself
// skip == 1 is this function that called runtime.Callers
// skip == 2 is the function that called this function, call it X
// skip == 3 is the function that called X
runtime.Callers(3, pc)
// by now, pc contains number x such that x is the pc of the caller
// we call runtime.CallersFrames to get the stack frames of all program counters in pc
frames := runtime.CallersFrames(pc)
// extract the caller's frame
f, _ := frames.Next()
callerName, callerFileName, callerLineNumber = f.Function, f.File, f.Line
return
}
func helloStranger() {
callerName, callerFileName, callerLineNum := whoCalledMe()
fmt.Printf("This function called me: %v\n", callerName)
fmt.Printf("It's located in: %v on line: %v\n", callerFileName, callerLineNum)
}
func main() {
helloStranger()
}
// output
// This function called me: main.main
// It's located in: C:/Users/x/y/z/main.go on line: 33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment