0

I'm trying to instance multiple objects of diverse types based on what you set on the config file.

Also, I'm trying to avoid to use the 'switch' statement with every Type of object that you can instance.

My original idea was use sort type of reflection, to create a object with a Type obtained from a config value.

For example: These is a YAML Config example

workers:
  - type: "Type1"
    parameters:
      param_0: "test"
      param_1: 1000
  - type: "Type2"
    parameters:
      param_0: "test"
      param_1: 1000

When these settings are analyzed; at run-time, the program must instance a "Type1" object with the "Parameters"; and then another instance of a "Type2" object with its "Parameters".

Please let me know if you need more information about it.

PD: Sorry for my bad English.

mvazquez
  • 71
  • 1
  • 3
  • 2
    Are the types pre-declared in the program or does the program not know anything about the types at startup? Go is a *statically* typed language so you cannot declare types at runtime, however given some kind of "registry" of available types, mapped by string keys, you could use reflection to instantiate values of those types. Here's an incomplete example: https://play.golang.org/p/yOLokgMAa1c – mkopriva Mar 28 '19 at 22:15
  • Possible duplicate of [is there a way to create an instance of a struct from a string?](https://stackoverflow.com/questions/23030884/is-there-a-way-to-create-an-instance-of-a-struct-from-a-string) – Nik Mar 28 '19 at 23:46

0 Answers0