Golang implements operations such as obtaining the current function name and file line number

Time:2021-7-31

Let’s look at the code directly~

//Gets the name of the running function
func runFuncName()string{
    pc := make([]uintptr,1)
    runtime.Callers(2,pc)
    f := runtime.FuncForPC(pc[0])
    return f.Name()
}
package main 
import(
    "fmt"
    "runtime"
)
 
//Gets the name of the running function
func runFuncName()string{
    pc := make([]uintptr,1)
    runtime.Callers(2,pc)
    f := runtime.FuncForPC(pc[0])
    return f.Name()
}
 
func test1(){
    i:=0
    fmt.Println("i =",i)
    fmt.Println("FuncName1 =",runFuncName())
}
 
func test2(){
    i:=1
    fmt.Println("i =",i)
    fmt.Println("FuncName2 =",runFuncName())
}
 
func main(){
    Fmt.println ("print function name in operation")
    test1()
    test2()
}

The runtime library of golang provides a caller function, which can return the file name and line number 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 calling method is as follows. The returned file is the absolute path and line is the line number. With this, you can add this record to your own log and other functions.


_, file, line, ok := runtime.Caller(1)

Supplement: go positioning function operation location (file name, function name, line)

Runtime. Caller () returns the program count PC, the file name executed and the number of lines in which the function is executed

Runtime. Funcforpc() is passed into PC to get the function pointer to run

file structure


- runtime
- -file1.go
- -file2.go
- -main.go

Main.go file


package main
import (
	"fmt"
	"path"
	"runtime"
)
func main(){
	name, funcName, line := f2(0)
	fmt.Printf("file:%v;function:%v;line:%d",name,funcName,line)
}
func getLocation(skip int)(fileName ,funcName string ,line int){
	pc, file, line, ok := runtime.Caller(skip)
	if !ok {
		fmt.Println("get info failed")
		return
	}
	fmt.Println(pc,file)
	fileName = path.Base(file)
	funcName = runtime.FuncForPC(pc).Name()
	return
}

File1.go file


package main
func f1(skip int)(fileName ,funcName string ,line int){
 fileName, funcName, line = getLocation(skip)
 return
}

File2.go file


package main
func f2(skip int)(fileName ,funcName string ,line int){
 return f1(skip)
}

When calling F2 in main.go file,


func main(){
 name, funcName, line := f2(3)
 fmt.Printf("file:%v;function:%v;line:%d",name,funcName,line)
 //output:file:main.go;function:main.main;line:10
}

F2 call F1, F1 call getlocation; F2 – > F1 – > getlocation has undergone three-level calls, so when 3 is passed in F2, the current execution location, function name and file name of the function will be returned

When 2 is passed in, the returned value is (file: File2. Go; function:main.f2; Line: 8) F2 function name, file location and file name

When 1 is passed in, it returns (file: file1. Go; function:main.f1; Line: 4) function name, file location and file name of F1 function

When 0 is passed in, it returns (file: main. Go; function:main.getLocation; Line: 16) function name, file location and file name of getlocation function

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If you have any mistakes or don’t consider completely, please don’t hesitate to comment.

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]