gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Golang 实现获取当前函数名称和文件行号等操作由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

大家还是直接看代码吧~ 。

?
1
2
3
4
5
6
7
// 获取正在运行的函数名
func runFuncName()string{
     pc := make([]uintptr,1)
     runtime.Callers(2,pc)
     f := runtime.FuncForPC(pc[0])
     return f.Name()
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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函数,可以返回运行时正在执行的文件名和行号:

?
1
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为行号。有了这个就可以在自己的日志等函数中添加这个记录了.

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

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

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

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

文件结构 。

?
1
2
3
4
- runtime
- -file1.go
- -file2.go
- -main.go

main.go文件 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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文件 。

?
1
2
3
4
5
package main
func f1(skip int)(fileName ,funcName string ,line int){
  fileName, funcName, line = getLocation(skip)
  return
}

file2.go文件 。

?
1
2
3
4
package main
func f2(skip int)(fileName ,funcName string ,line int){
  return f1(skip)
}

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

?
1
2
3
4
5
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函数所在函数名、文件位置、文件名 。

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

原文链接:https://blog.csdn.net/tiancityycf/article/details/102931566 。

最后此篇关于Golang 实现获取当前函数名称和文件行号等操作的文章就讲到这里了,如果你想了解更多关于Golang 实现获取当前函数名称和文件行号等操作的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com