在Go语言中,有没有一种方法可以获取源代码的文件名和行号?

huangapple go评论134阅读模式
英文:

Is there a way to get the source code filename and line number in Go?

问题

在C/C++中,你可以使用__FILE____LINE__来获取当前文件和行号。

Go语言提供类似的功能吗?

英文:

In C/C++ you can use __FILE__ and __LINE__ to get access to the current file and line number.

Does Go provide something similar?

答案1

得分: 8

确实可以:

http://golang.org/pkg/runtime/#Caller

runtime.Caller也可以用来获取调用函数的文件名/行号。

英文:

Indeed it does:

http://golang.org/pkg/runtime/#Caller

runtime.Caller can also be used to get the file name/line number of calling functions, too.

huangapple
  • 本文由 发表于 2011年2月10日 00:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/4947705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定