vendredi 19 juin 2015

Change struct tag (xml) at runtime

There's a struct:

type S struct {
  Value string `xml:value`
}

I want to encode the struct to an XML file. However, I want the element name of Value to be different in each file:

s1 := S{
  Value: "One"
}

should encode to:

<value>One</value>

and

s2 := S{
  Value: "Two"
}

should encode to:

<category>One</category>

So, I need either to change the XML element name somehow, or change the tag on the field. Is this possible?

I checked reflect (http://ift.tt/1GwbFeX), but since FieldByName returns a value type and there are no Set methods, I don't think it's possible to use reflection.





Aucun commentaire:

Enregistrer un commentaire