D
D
Dmitry Shnyrev2014-11-15 13:07:01
go
Dmitry Shnyrev, 2014-11-15 13:07:01

How do you work with dates in a database in Go?

Hello.
Share your recipes on how you work with dates in a database (mysql, postgres) in Go.
Here is my solution, please comment.
I use Postgres, I use fields of the timestamp type in the database
To get them into the structure I use the time.Time data type
Today I encountered the problem of
unsupported driver -> Scan pair: -> *
time.Time
found an option to use pq.NullTime Here is an example of how to use it:

var nt NullTime
err := db.QueryRow("SELECT time FROM foo WHERE id=?", id).Scan(&nt)
...
if nt.Valid {
   // use nt.Time
} else {
   // NULL value
}

Tell me, can there be simpler options for working with dates? For example / read that you can make an int field and store the time in Unix Timestamp
UPD: for example, what was written in https://github.com/coopernurse/gorp
To avoid any potential issues with timezone/DST, consider using an integer field for time data and storing UNIX time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Lerg, 2014-11-15
@Lerg

I like to store in strings, a field of type DATETIME, you can add NOT NULL. But it is also possible in unixtime.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question