博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang 获取当前文件名和行号的方法
阅读量:7000 次
发布时间:2019-06-27

本文共 733 字,大约阅读时间需要 2 分钟。

hot3.png

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

func (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)

转载于:https://my.oschina.net/u/1765027/blog/799964

你可能感兴趣的文章
使用OGG"Loading data from file to Replicat"的方法应该注意的问题:replicat进程是前台进程...
查看>>
SDUTOJ 2128 树结构练习——排序二叉树的中序遍历
查看>>
图文并茂排序与算法总结
查看>>
SCRUM的结构、范畴与本质
查看>>
最近火狐浏览器 总是“插件 adobe flash 已崩溃”
查看>>
PHP中将ip地址转成十进制数的两种实用方法
查看>>
H2 数据库
查看>>
开始学习Functional Programming
查看>>
Redis 和 Memcached 的区别
查看>>
jenkins和docker 在docker里运行jenkins
查看>>
【PHP升级】CentOS6.3编译安装 PHP5.4.38
查看>>
Lua 迭代器与closure
查看>>
mybatis_helloworld(2)_源码
查看>>
Atitit godaddy 文件权限 root权限设置
查看>>
完整部署CentOS7.2+OpenStack+kvm 云平台环境(3)--为虚拟机指定固定ip
查看>>
用rfkill命令管理蓝牙和wifi
查看>>
BLE 广播数据解析
查看>>
Maven初学
查看>>
Oracle用户密码过期和用户被锁解决方法【转】
查看>>
Android 解决Android的TextView和EditText换行问题
查看>>