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
+12.9k Golang : http.Get example
+7.8k Golang : Scan files for certain pattern and rename part of the files
+29.6k Golang : How to create new XML file ?
+11.3k Google Maps URL parameters configuration
+40.2k Golang : UDP client server read write example
+7.2k CloudFlare : Another way to get visitor's real IP address
+8.4k Golang : Auto-generate reply email with text/template package
+6.7k Golang : When to use make or new?
+7.8k Golang : Test if an input is an Armstrong number example
+21.2k Golang : For loop continue,break and range
+19.7k Golang : Set or Add HTTP Request Headers
+10.4k Golang : Convert file content to Hex