Ich habe folgende Struktur:Wie leere Felder in GO wegzulassen, wenn XML-Erzeugung
type CustomAttribute struct {
Id string `xml:"attribute-id,attr,omitempty"`
Values []string `xml:"value,omitempty"`
}
type Store struct {
XMLName xml.Name `xml:"store"`
Id string `xml:"store-id,attr,omitempty"`
Name string `xml:"name,omitempty"`
Address1 string `xml:"address1,omitempty"`
Address2 string `xml:"address2,omitempty"`
City string `xml:"city,omitempty"`
PostalCode string `xml:"postal-code,omitempty"`
StateCode string `xml:"state-code,omitempty"`
CountryCode string `xml:"country-code,omitempty"`
Phone string `xml:"phone,omitempty"`
Lat float64 `xml:"latitude,omitempty"`
Lng float64 `xml:"longitude,omitempty"`
CustomAttributes []CustomAttribute `xml:"custom-attributes>custom-attribute,omitempty"`
}
und dann initialisiere ich die Struktur wie folgt:
store := &Store{
Id: storeId,
Name: row[4],
Address1: row[5],
Address2: row[6],
City: row[7],
PostalCode: row[9],
StateCode: row[8],
CountryCode: row[11],
Phone: row[10],
}
So ist die Custom Array immer leer ist, und len (store.CustomAttributes) ist 0, also irgendeine Idee, warum das generierte XML immer noch das leere "custom-attributes" -Tag enthält?
<custom-attributes></custom-attributes>
Ich mag beide Lösungen (Ihre und die Antwort, die ich akzeptiere) und sie sind großartig zu lernen, wie Dinge implementiert werden können, aber da ich nur eine Antwort auswählen kann, wählte ich die am oberen Ende (basierend darauf, wie StackOverflow sie sortiert). Vielen Dank. – daniels