我可以检查main.main包的名称吗?

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

Can I introspect the name of the main.main package?

问题

这是一个相对独特的问题,但我目前正在尝试使用golang编写一个基于约定的设置存储库。如果我能够以编程方式确定想要存储某些内容的运行包名称(例如"github.net/author/projectname/pkg")并调用我的库函数,那将是一个很好的API增益。

使用Python可以通过inspect模块或者__main__.__file__以及对文件系统的查看来实现类似的功能。

英文:

This is a fairly niche problem, but I'm currently trying to write a conventions-based settings storage library with golang. It would be a great API boon if I could programmatically determine the running package name that wants to store something (eg "github.net/author/projectname/pkg") calling my library function.

With Python a similar thing could be achieved with the inspect module, or even with __main__.__file__ and a look at the file system.

答案1

得分: 4

如果您使用以下函数,您可以获得类似的信息:

代码可能如下所示:

pc, file, line, ok := runtime.Caller(1)
if !ok { /*failed*/ }
println(pc, file, line, ok)

f := runtime.FuncForPC(pc)
if f == nil { /*failed*/ }
println(f.Name())

如果我将上述代码(将第一行改为runtime.Caller(0))放入我已经安装在GOROOT中的(随机选择的)Go库中,它会打印:

134626026 /tmp/go-build223663414/github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go -4585 true
github.com/mattn/go-gtk/gtk.Init

或者它会打印:

134515752 /home/user/go/src/github.com/mattn/go-gtk/example/event/event.go 12 true
main.main

第一行和第二行的文件名似乎包含您要查找的信息。

存在两个问题:

  • 如果函数被编译器自动内联,可能会给出错误的结果。

  • 对于在main包中定义的任何函数F,函数名只是main.F。例如,如果从main()中调用runtime.Caller(0),即使main()函数在GOROOT/src/github.com/mattn/go-gtk/...中的Go文件中定义,函数名也是main.main。在这种情况下,runtime.Caller的输出比runtime.FuncForPC的输出更有用。

英文:

You can get similar information if you use the following functions:

The code may look like this:

pc, file, line, ok := runtime.Caller(1)
if !ok { /*failed*/ }
println(pc, file, line, ok)

f := runtime.FuncForPC(pc)
if f == nil { /*failed*/ }
println(f.Name())

If I put the above code (with the 1st line changed into runtime.Caller(0)) into a (randomly chosen) Go library which I have installed in GOROOT, it prints:

134626026 /tmp/go-build223663414/github.com/mattn/go-gtk/gtk/_obj/gtk.cgo1.go -4585 true
github.com/mattn/go-gtk/gtk.Init

Or it prints:

134515752 /home/user/go/src/github.com/mattn/go-gtk/example/event/event.go 12 true
main.main

The filename on the 1st line, and the 2nd line, seem to contain the information you are looking for.

There are two problems:

  • It may give incorrect result if functions are automatically inlined by the compiler

  • For any function F defined in package main, the function name is just main.F. For example, if runtime.Caller(0) is called from main(), the function name is main.main even if the main() function is defined in a Go file found in GOROOT/src/github.com/mattn/go-gtk/.... In this case, the output from runtime.Caller is more useful than the output from runtime.FuncForPC.

huangapple
  • 本文由 发表于 2012年2月18日 02:43:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/9333657.html
匿名

发表评论

匿名网友

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

确定