How can I create an object when I am only having its type in string? I am looking for something like:
type someStruct struct {}
resultObject := new \"someStruct
No...
Well, the answer is "yes, but" and it's a big but. There's no central registry of struct names in Go. You're not going to get a nice, clean standard library function called StructFromName(string)
which is probably what you were hoping for.
Instead, you have to write that mapping yourself, something like
func StringToStruct(name string) (interface{}, error) {
switch name {
case "SomeStruct":
return SomeStruct{}, nil
case "SomeOtherStruct":
return SomeOtherStruct{}, nil
case "subpackage.Struct":
return subpackage.Struct{}, nil
default:
return nil, fmt.Errorf("%s is not a known struct name", name)
}
}