防止json.marshal time.time删除尾随零

I have code similar to the following

package main

import (
    "fmt"
    "time"
    "encoding/json"
)

type Message struct {
    Time time.Time `json:"timestamp,omitempty"`
}

func main() {
    t, _ := time.Parse("2006-01-02T15:04:05.999Z07:00", "2017-05-01T15:04:05.630Z")
    msg := Message{
        Time: t,
    }
    bs, _ := json.Marshal(msg)
    fmt.Println(string(bs[:]))
}

This prints

{"timestamp":"2017-05-01T15:04:05.63Z"}

How can I make json marshalling keep the trailing 0? I.e., to print this?

{"timestamp":"2017-05-01T15:04:05.630Z"}

Edit:

Here's the playground https://play.golang.org/p/9p3kWeiwu2

time.Time always marshals to RFC 3339, only including sub-second precision if present: https://golang.org/pkg/time/#Time.MarshalJSON

You can write your own custom version using a named time.Time type, or you can define a custom marshal function for your structure, or you can make your structure hold a string in that place instead. In any case, if you want to use the same format, but including trailing zeros, you need to use a modified version of the RFC3339Nano constant.

Its value is: "2006-01-02T15:04:05.999999999Z07:00".

The 9's at the end mean "include until the rightmost non-zero value, omit after that". If you change those 9's to 0's, it will always include them. For example, if you always want millisecond precision (and nothing after that, regardless of whether it's non-zero or not), you would use:

"2006-01-02T15:04:05.000Z07:00"

If you feed that to Format() on your time.Time value, you'll get out the string you want, and can thus include it in the JSON.

Functioning example: https://play.golang.org/p/oqwnma6odw