Is there a way to get on runtime the name of current package?
package main
import "fmt"
func main() {
pkgName := {some magic here:)}
fmt.Println(pkgName)
}
… and the result should be "main"
Right now I'm using constant like:
package main
import "fmt"
const (
pkgName = "main"
)
func main() {
fmt.Println(pkgName)
}
but I'm curious if you can avoid this
Best Solution
There is no runtime or reflect method or function that provides the functionality that you are looking for.
The closest thing I could find is:
This would output:
You could also read the first line of the file and remove the "package" substring. (Not sure if it's the best idea)