globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Question: Why golang no response miliseconds time in json when equal 0 ? #240

Closed thuandoan274 closed 6 years ago

thuandoan274 commented 6 years ago

What did you do?

I'm using mongodb, and I have field createdAt like this

 "createdAt" : ISODate("2016-07-08T00:13:50.760Z")

What did you expect to see?

I want when I get json it will response like this

"timestamp": "2016-07-08T00:13:50.760Z"

What did you see instead?

I see it seem like golang omit the zero last

"timestamp": "2016-07-08T00:13:50.76Z"

It make mobile team I working with crash, because It's not follow the format the defined. Show anyone can help how can I response the zero miliseconds, I want a general solution for my project!

maitesin commented 6 years ago

Hi @thuandoan274,

The JSONMarshaller follows the RFC3339Nano standard that can handle nanoseconds: https://golang.org/pkg/time/#Time.MarshalJSON . However, take into account that in your case both are the same date since 50.760 seconds is the same value as 50.76 seconds. Therefore your trailing 0 will not be shown.

You can always write your own Marshaller to behave the way you want it to.

Since this is not a problem in the driver, but a design choice by the Golang language I will close this issue.

Oscar