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


Posted in Golang onMay 08, 2021

大家还是直接看代码吧~

// 获取正在运行的函数名
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函数所在函数名、文件位置、文件名

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

Golang 相关文章推荐
go原生库的中bytes.Buffer用法
Apr 25 Golang
Golang 正则匹配效率详解
Apr 25 Golang
解决golang在import自己的包报错的问题
Apr 29 Golang
golang 在windows中设置环境变量的操作
Apr 29 Golang
golang 实现Location跳转方式
May 02 Golang
GoLang中生成UUID唯一标识的实现
May 08 Golang
Go语言并发编程 sync.Once
Oct 16 Golang
Go语言grpc和protobuf
Apr 13 Golang
golang连接MySQl使用sqlx库
Apr 14 Golang
Golang MatrixOne使用介绍和汇编语法
Apr 19 Golang
Golang并发工具Singleflight
May 06 Golang
Golang 获取文件md5校验的方法以及效率对比
May 08 #Golang
GoLang中生成UUID唯一标识的实现
May 08 #Golang
聊聊golang中多个defer的执行顺序
May 08 #Golang
Golang全局变量加锁的问题解决
golang 实现并发求和
May 08 #Golang
golang中的并发和并行
May 08 #Golang
关于golang高并发的实现与注意事项说明
May 08 #Golang
You might like
php多文件上传下载示例分享
2014/02/20 PHP
php中magic_quotes_gpc对unserialize的影响分析
2014/12/16 PHP
PHP函数实现从一个文本字符串中提取关键字的方法
2015/07/01 PHP
php开发工具有哪五款
2015/11/09 PHP
PHP对象实例化单例方法
2017/01/19 PHP
CentOS系统中PHP安装扩展的方式汇总
2017/04/09 PHP
PHP从数组中删除元素的四种方法实例
2017/05/12 PHP
Yii框架ACF(accessController)简单权限控制操作示例
2019/04/26 PHP
checkbox 多选框 联动实现代码
2008/10/22 Javascript
深入理解JavaScript系列(9) 根本没有“JSON对象”这回事!
2012/01/15 Javascript
解析jQuery与其它js(Prototype)库兼容共存
2013/07/04 Javascript
封装了一个支持匿名函数的Javascript事件监听器
2014/06/05 Javascript
JS实现从表格中动态删除指定行的方法
2015/03/31 Javascript
jquery可定制的在线UEditor编辑器
2015/11/17 Javascript
JS实现简单易用的手机端浮动窗口显示效果
2016/09/07 Javascript
基于AngularJS实现iOS8自带的计算器
2016/09/12 Javascript
canvas绘制表盘时钟
2017/01/23 Javascript
微信小程序日期时间选择器使用方法
2018/02/01 Javascript
React Native基础入门之初步使用Flexbox布局
2018/07/02 Javascript
javascript实现简单留言板案例
2021/02/09 Javascript
[01:09:19]DOTA2-DPC中国联赛 正赛 VG vs Aster BO3 第二场 2月28日
2021/03/11 DOTA
利用Python的Twisted框架实现webshell密码扫描器的教程
2015/04/16 Python
深入解析Python中的descriptor描述器的作用及用法
2016/06/27 Python
Python学习小技巧之列表项的拼接
2017/05/20 Python
详解Python在七牛云平台的应用(一)
2017/12/05 Python
python 快速把超大txt文件转存为csv的实例
2018/10/26 Python
Python获取Redis所有Key以及内容的方法
2019/02/19 Python
利用django创建一个简易的博客网站的示例
2020/09/29 Python
就业推荐自我鉴定
2013/10/06 职场文书
水务局局长岗位职责
2013/11/28 职场文书
电子商务应届生自我鉴定
2014/01/13 职场文书
烹调加工管理制度
2014/02/04 职场文书
2015年度销售个人工作总结
2015/03/31 职场文书
2015年房产销售工作总结范文
2015/05/22 职场文书
女儿满月酒致辞
2015/07/29 职场文书
python实现的web监控系统
2021/04/27 Python