You can use time in Golang. The time data type to hold isodate times in MongoDB.
G
Type Model struct {uploaddate time. Time ' Bson: ' uploaddate ' '}m: = model{}if Err: = C.find (nil). Select ({"_id": 0, "uploaddate": 1}). One (&m); Err! = Nil {fmt. Printf ("Failed to find date, error%s\n", err) OS. Exit (1)}fmt. Printf ("%+v\n", m)
From the output you can see that the time format of the Golang output is the CST time zone, Golang in the process of converting the ISO time to CST times, from the time panel is also more than 8 hours in MongoDB, this is normal.
So Golang do you need to convert the timestamp when you do the insert operation or time comparison operation? The answer is no need, look at the insert example.
Reinsert a record with the time field of the current period, which can be passed in Golang. Now to get the current time, view the output to see the time format as CST.
Now: = time. Now () Fmt. Printf ("%+v\n", now)//output:2016-05-12 14:34:00.998011694 +0800 csterr = C.insert (Model{time:now}) if err! = Nil {P Anic (ERR)}
You can see the automatic conversion of time stored into MongoDB for ISO time, 8 hours less time. A summary is that the time interaction in Golang and MongoDB does not need to be considered for additional things because the drivers are converted.
Sometimes we will take time. Time is stored as a string, so you need to convert it when you interact with MongoDB. Time format
TimeString: = "2016-05-12 14:34:00.998011694 +0800 CST" T, err: = time. Parse ("2006-01-02 15:04:05.999999999-0700 MST", timestring) if err! = Nil {panic (err)}fmt. Printf ("%+v\n", T)
What's hard to understand in the code is time. The first argument to parse, which is actually a definition in Golang, looks at time in detail. The source of the time.string () will understand.
This article from the "Linux related" blog, reproduced please contact the author!
Golang parsing the isodate type in MongoDB