jeudi 5 avril 2018

How to dynamically create a struct with one less property?

Is there a way to copy a generic struct (i.e. a struct whose property names are unknown) and skip a single, known property?

Here is what I know:

  • The parameter to my function--I will call the parameter myData-- is of type interface{}.
  • myData is a struct.
  • myData has a known property path.
  • myData has anywhere from 0 to 6 or so other properties, none of which are known a priori.
  • Once I remove that path property, then the “leftover” is one of say 30 possible struct types.

So I want to strip path out of myData (or more accurately make a copy that omits path) so that various bits of generated code that try to coerce the struct to one of its possible types will be able to succeed.

I have found examples of copying a struct by reflection, but they typically create an empty struct of the same underlying type, then fill it in. So is it even possible to delete a property as I have outlined...?





Aucun commentaire:

Enregistrer un commentaire