I'm parsing XML, and at most every level of the document, there's a description
.
Here's an toy example:
<obj>
<description>outer object</description>
<subobjA>
<description>first kind of subobject</description>
<foo>some goop</foo>
</subobjA>
<subobjB>
<description>second kind of subobject</description>
<bar>some other goop</bar>
</subobjB>
</obj>
This means that every struct involved has an identical Description
member, with an identical tag `xml:"description,omitempty"`
.
Here's functioning code: http://play.golang.org/p/1-co6Qcm8d
I'd rather the Description tags be DRY. The obvious thing to want to do is something like:
type Description string `xml:"description,omitempty"`
and then use the type Description
throughout. However, only struct members can have tags. See http://play.golang.org/p/p83UrhrN4u for what I want to write; it doesn't compile.
One could create a Description
struct and embed it repeatedly, but that adds a layer of indirection when accessing.
Is there another way to go about this?