How to discover all package types at runtime?

后端 未结 5 1519
醉酒成梦
醉酒成梦 2020-12-14 02:55

As far as I\'m aware (see here, and here) there is no type discovery mechanism in the reflect package, which expects that you already have an instance of the type or value y

5条回答
  •  伪装坚强ぢ
    2020-12-14 03:39

    Unfortunately, I don't think this is possible. Packages are not "actionable" in Go, you can't "call a function" on it. You can't call a function on a type either, but you can call reflect.TypeOf on an instance of the type and get reflect.Type which is a runtime abstraction of a type. There just isn't such mechanism for packages, there isn't a reflect.Package.

    With that said, you could file an issue about the absence of (and practicality of adding) reflect.PackageOf etc.

提交回复
热议问题