【发布时间】:2013-06-22 11:24:28
【问题描述】:
我有通过 REST API 检索的 XML 数据,我将其解组到 GO 结构中。其中一个字段是日期字段,但是 API 返回的日期格式与默认时间不匹配。时间解析格式因此解组失败。
有什么方法可以指定 unmarshal 函数在 time.Time 解析中使用哪种日期格式?我想使用正确定义的类型并使用字符串来保存日期时间字段感觉不对。
示例结构:
type Transaction struct {
Id int64 `xml:"sequencenumber"`
ReferenceNumber string `xml:"ourref"`
Description string `xml:"description"`
Type string `xml:"type"`
CustomerID string `xml:"namecode"`
DateEntered time.Time `xml:"enterdate"` //this is the field in question
Gross float64 `xml:"gross"`
Container TransactionDetailContainer `xml:"subfile"`
}
返回的日期格式为“yyyymmdd”。
【问题讨论】:
-
这可能会有所帮助,即使它只处理编组。 groups.google.com/forum/#!topic/golang-nuts/IM3ZIcYXbz4
标签: xml-parsing go unmarshalling