How can I find the package name from a given call at runtime?
For logging purposes, I want to write a function that will print the package name. I can do this for the directory name:
// 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)
... but can't find a way for the package name (the package name can be different from the directory name).
+3
source to share
1 answer
I faced similar issue - from the package path, how do you get the package name. The best solution I have found is to run the "go list" command. Not perfect, but I came up with something elsewhere.
In my case, I also had the problem that sometimes the package is an empty directory. Without source files, "go list" throws an error, so I added a function to generate a sensible package name from that path.
Here's the code:
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
}
+1
source to share