duandian4501 2012-02-17 18:43
浏览 42
已采纳

我可以内省main.main软件包的名称吗?

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条回答 默认 最新

  • dsegw3424 2012-02-17 19:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?