2

I am using the go/importer package to get an instance of go/types.Struct:

pkg, _ := importer.Default().Import("some/package")
structType := pkg.Scope().Lookup("StructName").Type()

I would like to convert this type into a reflect.Type so that I can instantiate a new instance. Is this possible to do? Is there a way to go from a package name / struct name into a new instance (assuming the code executes in a Go environment with source and GOPATH set properly)?

Max
  • 15,157
  • 17
  • 82
  • 127
  • The runtime and the reflect package do not provide a way to get a reflect.Type given the package and type names. Typical solution is for app to maintain map of name to reflect.Type. Possible duplicate of https://stackoverflow.com/questions/23030884/is-there-a-way-to-create-an-instance-of-a-struct-from-a-string – Charlie Tumahai Jul 15 '18 at 14:47
  • I was hoping to accept the package name / type name as user input so maintaining a map would not be possible. Do you have any other ideas how to solve this problem, so do the types, build, and importer packages not provide anyway to make this possible? – Max Jul 15 '18 at 14:50
  • 1
    It's not possible to get a reflect.Type from the go/types or related packages because the runtime does not have a registry of types. Describe the higher-level problem you are trying to solve. – Charlie Tumahai Jul 15 '18 at 15:00
  • I would like to accept a type from the user via a configuration file and then be able to instantiate that type. – Max Jul 15 '18 at 16:03
  • 1
    @Max and how are you using the model that the type relies in? Do you import it in your code or do you use `plugin` or other loading techniques? – leaf bebop Jul 15 '18 at 16:05
  • @leafbebop right now I write a CLI that runs inside a Go environment, i.e. inside a `src` directory in the `GOPATH`. I'm getting the type information from the `go/importer` package using the type information supplied by the user. I don't directly import the type at compile time of the CLI – Max Jul 15 '18 at 16:10
  • @Max You have three options to use a type of external packages: 1. import it at compile time 2. use `plugin` or other loading mechenism (requires to compile the other library in certain mood) 3. Interept that package and then use the interepted result. The 3rd option is extremely compllicated. – leaf bebop Jul 15 '18 at 16:16
  • 1
    How are you planning to use that type? If it just stores data, 3rd option can be much simpler. If you are going to use its methods, and you are really willing to try 3rd option, you can dig into https://github.com/go-interpreter/wagon. – leaf bebop Jul 15 '18 at 16:20
  • @leafbebop thanks for the info. I would like to call the methods as well as access the fields in the struct via reflection. I will look into those options. Do you have any information on the `plugin` loading mechanism? – Max Jul 15 '18 at 16:56
  • 1
    [Official approach](https://golang.org/pkg/plugin/) and [third party loader](https://github.com/dearplain/goloader) – leaf bebop Jul 15 '18 at 17:04

0 Answers0