如何在运行时从给定的调用中查找包名?

时间:2015-07-22 15:35:40

标签: go stack-trace

为了记录目的,我想写一个打印包名的函数。 我可以为目录名称执行此操作:

// 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) 

...但找不到包名的方法(包名可以与目录名不同)。

1 个答案:

答案 0 :(得分:1)

我遇到了类似的问题 - 从包路径中如何获得包名称。我发现的最佳解决方案是执行" 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
}