As shown below:
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Parameters: skip is the number of stack frames to be increased, 0-current function, 1-previous layer function,...
Return value:
pc is uintptr, which returns a function pointer
file is the file name directory where the function is located
Line number
OK Can information be obtained
Example:
We print relevant information with skip of 0-3 respectively
package main import ( "fmt" "runtime" ) func main() { for i := 0 ; i< 4; i++ { test(i) } } func test(skip int) { call(skip) } func call(skip int) { pc,file,line,ok := (skip) pcName := (pc).Name() //Get the function name (("%v %s %d %t %s",pc,file,line,ok,pcName)) }
result:
4887700 D:/GoProject/src /test/ 19 true 4887585 D:/GoProject/src/ test/ 15 true 4887481 D:/GoProject/src /test/ 10 true 4383501 C:/Go/src/runtime/ 198 true
The analysis results can be seen
0-3 respectively, the current function is the previous caller of the current function,...
Supplement: golang prints all runtime call stacks
I won't say much nonsense, let's just read the code~
import "runtime" buf := make([]byte, 1 << 20) (buf, true) ("\n%s", buf)
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.