Everyone should just read the code ~
// Get the running function namefunc runFuncName()string{ pc := make([]uintptr,1) (2,pc) f := (pc[0]) return () }
package main import( "fmt" "runtime" ) // Get the running function namefunc runFuncName()string{ pc := make([]uintptr,1) (2,pc) f := (pc[0]) return () } func test1(){ i:=0 ("i =",i) ("FuncName1 =",runFuncName()) } func test2(){ i:=1 ("i =",i) ("FuncName2 =",runFuncName()) } func main(){ ("Print the running function name") test1() test2() }
golang's runtime library provides Caller function, which can return the file name and line number that is being executed at runtime:
func Caller(skip int) (pc uintptr, file string, line int, ok bool) {
Caller reports file and line number information about function invocations on the calling goroutine's stack. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller. (For historical reasons the meaning of skip differs between Caller and Callers.) The return values report the program counter, file name, and line number within the file of the corresponding call. The boolean ok is false if it was not possible to recover the information.
The call method is as follows, the returned file is the absolute path and the line is the line number. With this, you can add this record to your own logs and other functions.
_, file, line, ok := (1)
Supplement: go locate function operation location (file name, function name, line)
() Return the function executor's program count, the execution file name and the number of lines
() Pass in pc and get the running function pointer
File structure
- runtime - - - - - -
document
package main import ( "fmt" "path" "runtime" ) func main(){ name, funcName, line := f2(0) ("file:%v;function:%v;line:%d",name,funcName,line) } func getLocation(skip int)(fileName ,funcName string ,line int){ pc, file, line, ok := (skip) if !ok { ("get info failed") return } (pc,file) fileName = (file) funcName = (pc).Name() return }
document
package main func f1(skip int)(fileName ,funcName string ,line int){ fileName, funcName, line = getLocation(skip) return }
document
package main func f2(skip int)(fileName ,funcName string ,line int){ return f1(skip) }
When f2 is called in file
func main(){ name, funcName, line := f2(3) ("file:%v;function:%v;line:%d",name,funcName,line) //output:file:;function:;line:10 }
f2 calls f1, f1 calls getLocation; f2->f1->getLocation undergoes three-layer calls, so when 3 is passed in f2, the current execution location of the function, the function name and file name of the returned function.
When 2 is passed, the return is (file:;function:main.f2;line:8) The function name, file location, and file name where the f2 function is located
When 1 is passed, the return is (file:;function:main.f1;line:4) The function name, file location, and file name where the f1 function is located
When 0 is passed, the return is (file:;function:;line:16) The function name, file location, and file name where the getLocation function is located
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.