vendredi 21 avril 2023

Declare type dynamically in GoLang

I have a field struct type:

{
Name: "fieldA",
Type: "string",
}

and an array of this filed type:

[{
Name: "fieldA"
Type: "string"
},
{
Name: "filedB",
Type: "int",
}
...

This array may change or grow later.

Now I want to define a new struct type based on this array in runtime, like this:

type myStruct struct {
fieldA string,
fieldB int,
...
}

I think using reflection, I can get a myStruct instance by calling reflect.StructOf() but can I get the type? Is this possible?

Thanks





Aucun commentaire:

Enregistrer un commentaire