我尝试解组一个xml数组,其中我想省略空元素。在解组XML元素时忽略空数组元素
我期望以下代码打印2,因为第二个条元素是空的。而是打印3。
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))
}
转到操场链接:https://play.golang.org/p/co8QxkyKTv
的'omitempty'只能如果没有' '在你的''里面。 –
TheHippo