【发布时间】:2016-04-13 19:30:14
【问题描述】:
我正在尝试格式化自定义时间类型 Date,它实现了 Marshaler 接口,并在编写为 XML 时简单地将自身格式化为“2006-01-02”。
type Person struct {
...
DateOfBirth Date `xml:"DOB,attr"`
...
}
type Date time.Time
func (d Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
dateString := fmt.Sprintf("\"%v\"", time.Time(d).Format("2006-01-02"))
e.EncodeElement(dateString, start)
return nil
}
我使用 this SO 作为参考,但抛出了错误 - &xml.UnsupportedTypeError{Type:(*reflect.rtype)}。
我错过了什么,有什么想法吗?
【问题讨论】: