I am able to print the name of the caller function, but I want to extract the parameter of the caller function, is it possible to do that in Go.
I’m attaching my code here for your reference.
package main
import (
"fmt"
"runtime"
)
func foo() {
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
var funcName string
if ok && details != nil {
funcName = details.Name()[5:]
// I am able to print the funcName but I want the parameter of caller
// function. callerFunction's parameter (x)
fmt.Printf("called from %s\n", funcName)
}
}
func callerFunction(x int)(int){
foo()
return x + 2
}
func main() {
z := callerFunction(5)
fmt.Printf("Value of z is %d\n",z)
}
The output of the above code is:
called from callerFunction
Value of z is 7