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
+37.5k Upload multiple files with Go
+17k Golang : Get number of CPU cores
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+6.9k Golang : Fibonacci number generator examples
+21.1k Golang : For loop continue,break and range
+11.5k Golang : Generate DSA private, public key and PEM files example
+16.9k Golang : Set up source IP address before making HTTP request
+7.5k Golang : Dealing with struct's private part
+11.2k Google Maps URL parameters configuration
+38.1k Golang : Read a text file and replace certain words
+10k Golang : Setting variable value with ldflags
+12.5k Golang : "https://" not allowed in import path