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 相关文章推荐
golang interface判断为空nil的实现代码
Apr 24 Golang
Go语言使用select{}阻塞main函数介绍
Apr 25 Golang
go语言求任意类型切片的长度操作
Apr 26 Golang
Go语言中break label与goto label的区别
Apr 28 Golang
golang 实现Location跳转方式
May 02 Golang
解决Golang中goroutine执行速度的问题
May 02 Golang
Golang 获取文件md5校验的方法以及效率对比
May 08 Golang
Go语言实现Base64、Base58编码与解码
Jul 26 Golang
Golang表示枚举类型的详细讲解
Sep 04 Golang
Go语言基础map用法及示例详解
Nov 17 Golang
Go Grpc Gateway兼容HTTP协议文档自动生成网关
Jun 16 Golang
go goth封装第三方认证库示例详解
Aug 14 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
一条久听不愿放下的DIY森海MX500,三言两语话神奇
2021/03/02 无线电
Apache2中实现多网站域名绑定的实现方法
2011/06/01 PHP
PHP遍历数组的几种方法
2012/03/22 PHP
PHP实现多图上传(结合uploadify插件)思路分析
2016/11/30 PHP
PHP获取真实客户端的真实IP
2017/03/07 PHP
php post json参数的传递和接收处理方法
2018/05/31 PHP
关于javascript中dataset的问题小结
2015/11/16 Javascript
安装使用Mongoose配合Node.js操作MongoDB的基础教程
2016/03/01 Javascript
EasyUI 中combotree 默认不能选择父节点的实现方法
2016/11/07 Javascript
Bootstrap实现导航栏的2种方式
2016/11/28 Javascript
Spring Boot+AngularJS+BootStrap实现进度条示例代码
2017/03/02 Javascript
jQuery正则验证注册页面经典实例
2017/06/10 jQuery
angular select 默认值设置方法
2017/06/23 Javascript
React Native使用Modal自定义分享界面的示例代码
2017/10/31 Javascript
利用jQuery+localStorage实现一个简易的计时器示例代码
2017/12/25 jQuery
node.js通过axios实现网络请求的方法
2018/03/05 Javascript
angular4强制刷新视图的方法
2018/10/09 Javascript
js实现上传按钮并显示缩略图小轮子
2020/05/04 Javascript
深入理解 ES6中的 Reflect用法
2020/07/18 Javascript
在vue项目中封装echarts的步骤
2020/12/25 Vue.js
Python使用scrapy抓取网站sitemap信息的方法
2015/04/08 Python
Python读取sqlite数据库文件的方法分析
2017/08/07 Python
pytorch: tensor类型的构建与相互转换实例
2018/07/26 Python
通过实例解析Python return运行原理
2020/03/04 Python
python cookie反爬处理的实现
2020/11/01 Python
Python中读取文件名中的数字的实例详解
2020/12/25 Python
详解pycharm的python包opencv(cv2)无代码提示问题的解决
2021/01/29 Python
美国电子元器件分销商:Newark element14
2018/01/13 全球购物
毕业生求职自荐信怎么写
2014/01/08 职场文书
机电专业大学生职业规划书范文
2014/02/25 职场文书
未受刑事制裁公证证明
2014/09/20 职场文书
2015年秋季小班开学寄语
2015/05/27 职场文书
大学校园餐饮创业计划书
2019/08/07 职场文书
职场新人知识:如何制定一份合理的工作计划?
2019/09/11 职场文书
MySQL中distinct和count(*)的使用方法比较
2021/05/26 MySQL
开发微信小程序之WXSS样式教程
2022/04/18 HTML / CSS