我正在解组来自 JSON API 的 GET 请求。其中一些字段是 Unix 时间戳(以毫秒为单位)。对于这些字段,我编写了一个处理解组的方法。为此,我需要创建一个新的本地类型Datetime。type Datetime time.Timetype Response struct { Status string `json:"status"` CreatedAt Datetime `json:"created_at"`}// Handles unmarshalling of Time from the APIfunc (dt *Datetime) UnmarshalJSON(b []byte) error { var unixTimeMs int64 if err := json.Unmarshal(b, &unixTimeMs); err != nil { return err } *dt = Datetime(time.Unix(0, unixTimeMs*1000000)) return nil}现在我有了这个新的 Response 对象,我想time.Time在 Datetime 上调用方法。我尝试过强制转换对象(即 response.CreatedAt.(time.Time)),但这会导致恐慌。我怎样才能调用类似time.Time#stringon 的方法Datetime?
- 1 回答
- 0 关注
- 183 浏览
添加回答
举报
0/150
提交
取消
