Golang函数名称文件行号获取 Golang 实现获取当前函数名称和文件行号等操作

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Golang函数名称文件行号获取 Golang 实现获取当前函数名称和文件行号等操作

私念   2021-05-08 我要评论
想了解Golang 实现获取当前函数名称和文件行号等操作的相关内容吗,私念在本文为您仔细讲解Golang函数名称文件行号获取的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Golang,当前函数名称,文件行号,下面大家一起来学习吧。

大家还是直接看代码吧~

// 获取正在运行的函数名
func runFuncName()string{
    pc := make([]uintptr,1)
    runtime.Callers(2,pc)
    f := runtime.FuncForPC(pc[0])
    return f.Name()
}
package main 
import(
    "fmt"
    "runtime"
)
 
// 获取正在运行的函数名
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("打印运行中的函数名")
    test1()
    test2()
}

golang 的runtime库,提供Caller函数,可以返回运行时正在执行的文件名和行号:

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.

调用方法如下,返回的file为绝对路径,line为行号。有了这个就可以在自己的日志等函数中添加这个记录了。

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

补充:go 定位函数操作位置(文件名、函数名、所在行)

runtime.Caller()返回函数执行程序计数pc、执行的文件名和所在行数

runtime.FuncForPC()传入pc,得到运行的函数指针

文件结构

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

main.go文件

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文件

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

file2.go文件

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

当在main.go文件中调用f2时

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调取f1,f1调取getLocation;f2->f1->getLocation经历了三层调用,所以在f2中传入3时,返回的当前该函数的执行位置及所在函数名、所在文件名

当传入2时,返回的是(file:file2.go;function:main.f2;line:8)f2函数所在函数名、文件位置、文件名

当传入1时,返回的是(file:file1.go;function:main.f1;line:4)f1函数所在函数名、文件位置、文件名

当传入0时,返回的是(file:main.go;function:main.getLocation;line:16)getLocation函数所在函数名、文件位置、文件名

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们