I try to unmarshal an xml array where I want to omit empty elements.
I would expect the following code to print 2, as the second bar element is empty. Instead 3 is printed.
package main
import (
"fmt"
"encoding/xml"
"bytes"
)
type foo struct {
Bars []string `xml:"bar,omitempty"`
}
func main() {
xmlStr := `
<foo>
<bar>1</bar>
<bar></bar>
<bar>2</bar>
</foo>`
var f foo
xml.NewDecoder(bytes.NewBufferString(xmlStr)).Decode(&f)
fmt.Println(len(f.Bars))
}
Go playground link: https://play.golang.org/p/co8QxkyKTv