Как сказано в комментарии, вы можете использовать карту для своих типов.Похоже на это.Функция factory возвращает экземпляр, если тип существует, или nil, если его нет.основной пакет
import (
"fmt"
"reflect"
)
type SomeType struct{ Something string }
type AnotherType struct{}
type YetAnotherType struct{}
var typemap = map[string]interface{}{
"SomeType": SomeType{ Something: "something" },
"AnotherType": AnotherType{},
"YetAnotherType": YetAnotherType{},
}
func factory(s string) interface{} {
t, ok := typemap[s]
if ok {
return reflect.ValueOf(t)
}
return nil
}
func main() {
fmt.Printf("%#v\n", factory("SomeType"))
fmt.Printf("%#v\n", factory("NoType"))
}
ссылка на игровую площадку