您可以做的是,将time.Time包装为您自己的自定义类型,并使其实现
Marshaler接口:
type Marshaler interface { MarshalJSON() ([]byte, error)}因此,您要做的是:
type JSonTime time.Timefunc (t JSONTime)MarshalJSON() ([]byte, error) { //do your serializing here stamp := fmt.Sprintf(""%s"", time.Time(t).Format("Mon Jan _2")) return []byte(stamp), nil}并制作文件:
type document struct { Name string Content string Stamp JSonTime Author string}并使您的初始化看起来像:
testDoc := model.document{"Meeting Notes", "These are some notes", JSonTime(time.Now()), "Bacon"}就是这样。如果您想拆封,也可以使用该
Unmarshaler界面。



