出于日志记录的目的,我想编写一个函数来打印包名。 我可以为目录名这样做:
// file is the full file name
// 4 - how many calls we want to go up in a stack trace.
_, file, line, ok := runtime.Caller(4)
...但找不到包名的方法(包名可以与目录名不同)。
最佳答案
我遇到了类似的问题 - 如何从包路径中获取包名称。我找到的最佳解决方案是执行“go list”命令。不理想,但我在其他地方一片空白。
在我的例子中,我也有一个问题,有时包是一个空目录。由于没有源文件,“go list”会抛出一个错误,所以我添加了一个函数来根据路径创建一个合理的包名。
代码如下:
func getPackageName(path string) string {
output, err := exec.Command("go", "list", "-f", "{{.Name}}", path).CombinedOutput()
if err != nil {
return guessPackageName(path)
}
return strings.TrimSpace(string(output))
}
func guessPackageName(path string) string {
preferred := path
if strings.HasSuffix(preferred, "/") {
// training slashes are usually tolerated, so we can get rid of one if it exists
preferred = preferred[:len(preferred)-1]
}
if strings.Contains(preferred, "/") {
// if the path contains a "/", use the last part
preferred = preferred[strings.LastIndex(preferred, "/")+1:]
}
if strings.Contains(preferred, "-") {
// the name usually follows a hyphen - e.g. github.com/foo/go-bar
// if the package name contains a "-", use the last part
preferred = preferred[strings.LastIndex(preferred, "-")+1:]
}
if strings.Contains(preferred, ".") {
// dot is commonly usually used as a version - e.g. github.com/foo/bar.v1
// if the package name contains a ".", use the first part
preferred = preferred[:strings.LastIndex(preferred, ".")]
}
return preferred
}
关于go - 如何在运行时从给定调用中查找包名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31567734/