1 回答

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
“是否有可能使用標(biāo)準(zhǔn)xml包實(shí)現(xiàn)我的首選行為?如果是,是否需要為其引入新類型和自定義封送 /XML() 方法?-- 是的,是的。
例如:
type Bool struct {
Bool bool
IsValid bool
}
func (b Bool) MarshalXML(e *xml.Encoder, se xml.StartElement) error {
if b.IsValid {
return e.EncodeElement(b.Bool, se)
}
return e.EncodeElement("", se)
}
type OptionalBool struct {
Bool bool
IsValid bool
}
func (b OptionalBool) MarshalXML(e *xml.Encoder, se xml.StartElement) error {
if b.IsValid {
return e.EncodeElement(b.Bool, se)
}
return nil
}
https://play.golang.org/p/C2fuBfv69Ny
- 1 回答
- 0 關(guān)注
- 102 瀏覽
添加回答
舉報(bào)