Answer the question
In order to leave comments, you need to log in
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
}
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
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 questionAsk a Question
731 491 924 answers to any question