Override the layout used by json.Marshal to format time.Time

前端 未结 3 1726
深忆病人
深忆病人 2021-01-05 07:53

In Golang, is there a way to make the generic encoding/json Marshal to use a different layout when Marshaling the time.Time fields?

Basical

3条回答
  •  有刺的猬
    2021-01-05 08:34

    First, I highly recommend against using a time format other than the default RFC3339. It's a good time format, and can be parsed by any number of languages, so unless you are needing a different format because somebody else's API requires it, it's probably best to use the default.

    But, I've had to solve this problem in consuming other people's APIs, so here is one solution that shifts the bulk of the work to the Marshal/Unmarshal step, and leaves you with an ideal structure: http://play.golang.org/p/DKaTbV2Zvl

提交回复
热议问题