我有一个这样的自定义类型:
type Timestamp struct {
Time time.Time
}
// some more methods...
现在,当我使用fmt
打印它的一个实例时:
test := Timestamp{
Time: time.Now(),
}
fmt.Println("TEST:", test)
输出为:
TEST: {2009-11-10 23:00:00 +0000 UTC m=+0.000000001}
如何在应使用2009-11-10T23:00:00Z
函数(fmt
等进行打印的情况下)向自定义类型添加自定义格式以漂亮地打印输出,如Println
? / p>
答案 0 :(得分:0)
就像添加此功能一样简单:
func (ts Timestamp) Format(f fmt.State, c rune) {
f.Write([]byte(ts.Time.Format(time.RFC3339)))
}
输出:
TEST: 2020-05-01T08:25:14Z