Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get name of current package in go?

Tags:

go

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

like image 922
Kamil Dziedzic Avatar asked Aug 12 '14 11:08

Kamil Dziedzic


2 Answers

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:

package main  import (     "azul3d.org/lmath.v1"     "fmt"     "reflect" )  type Empty struct{}  func main() {     fmt.Println(reflect.TypeOf(Empty{}).PkgPath())     fmt.Println(reflect.TypeOf(lmath.Vec3{0, 0, 0}).PkgPath()) } 

This would output:

main azul3d.org/lmath.v1 

You could also read the first line of the file and remove the "package" substring. (Not sure if it's the best idea)

package main  import (     "bufio"     "bytes"     "fmt"     "os" )  func main() {     file, err := os.Open("so.go")     if err != nil {         panic(err)     }     r := bufio.NewReader(file)     line, _, err := r.ReadLine()     if err != nil {         panic(err)     }     packageName := bytes.TrimPrefix(line, []byte("package "))     fmt.Println(string(packageName)) } 
like image 122
Marc Avatar answered Oct 12 '22 18:10

Marc


Here a part of my logger package. It retrieves information about the caller of the logging function to show it later in the output.

func retrieveCallInfo() *callInfo {     pc, file, line, _ := runtime.Caller(2)     _, fileName := path.Split(file)     parts := strings.Split(runtime.FuncForPC(pc).Name(), ".")     pl := len(parts)     packageName := ""     funcName := parts[pl-1]      if parts[pl-2][0] == '(' {         funcName = parts[pl-2] + "." + funcName         packageName = strings.Join(parts[0:pl-2], ".")     } else {         packageName = strings.Join(parts[0:pl-1], ".")     }      return &callInfo{         packageName: packageName,         fileName:    fileName,         funcName:    funcName,         line:        line,     } } 

As you can see it returns the package name too.

like image 42
themue Avatar answered Oct 12 '22 18:10

themue