samedi 29 février 2020

How to get the package (path) of a value?

I would like to get the path of package which defined a value. Given:

package main

import (
    "fmt"
    "reflect"

    "github.com/clipperhouse/jargon/stemmer"
)

func main() {
    valOf := reflect.ValueOf(stemmer.English)
    fmt.Println(valOf)
}

... my desired result is the string github.com/clipperhouse/jargon/stemmer. Here is a playground.

I have tried variations of Type().PkgPath, but I am not interested in the type (which may be defined in yet another package), but rather the package that defined this particular instance value (stemmer.English).





Aucun commentaire:

Enregistrer un commentaire