Golang : Error reading timestamp with GORM or SQL driver
Problem :
It is common for application to read from database these days and sometimes a missing parameter in the database connection configuration can cause chaos. For instance, while attempting to read time stamp data from database, you get these funky errors :
(sql: Scan error on column index 2: unsupported driver -> Scan pair: []uint8 -> *time.Time)
and when you try to read time stamp from your database(MySQL, SQLite, etc), it becomes :
0001-01-01 00:00:00 +0000 UTC
Solution :
Make sure that your database connection is initialized with parseTime=true
parameter.
For example :
dbConn, err := gorm.Open("mysql", "username:password@tcp(xx.xx0.x65.xx4:3306)/dbname?charset=utf8&parseTime=true")
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.5k Golang : List available AWS regions
+33.5k Golang : convert(cast) bytes to string
+9.4k Javascript : Read/parse JSON data from HTTP response
+28.2k Get file path of temporary file in Go
+22.4k Golang : Round float to precision example
+13.6k Golang : Human readable time elapsed format such as 5 days ago
+13.4k Golang : Tutorial on loading GOB and PEM files
+16.9k Golang : How to tell if a file is compressed either gzip or zip ?
+7.1k Golang : Example of custom handler for Gorilla's Path usage.
+8.9k Golang : Write multiple lines or divide string into multiple lines
+7.9k Golang : Tell color name with OpenCV example