我嘗試解組一個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