mercredi 1 novembre 2023

getting all export type info using reflection for golang package

Project structure is like below


root
/ package1
executor struct
/ package2
model struct

inside package1 want to programmatically read all exported types of package2. I believe it is doable using reflection. Facing issue in finding the import path of package2.

Tried using https://pkg.go.dev/go/importer#pkg-functions importer package using

pkg:= importer.Default().Import("model") this does not work.

Tried using PkgPath

pkgPath := reflect.TypeOf(model.Policy{}).PkgPath()
cfg := &packages.Config{
        Mode:  packages.NeedTypes | packages.NeedTypesInfo,
        Tests: false,
        Env:   append(os.Environ(), "GO111MODULE=off", "USE_SYSTEM_GO=1",
    }
pkgs, err := packages.Load(cfg, pkgPath) 
pkg:= importer.Default().Import("model") this does not work.

this also does not work.

This is bazel based build system.





Aucun commentaire:

Enregistrer un commentaire