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
+9k Golang : Get SPF and DMARC from email headers to fight spam
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+8.2k Golang : Metaprogramming example of wrapping a function
+32.1k Golang : Validate email address with regular expression
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+48.6k Golang : Upload file from web browser to server
+12.3k Golang : How to check if a string starts or ends with certain characters or words?
+9.6k Golang : Quadratic example
+23.2k Golang : Print out struct values in string format
+13.1k Golang : Handle or parse date string with Z suffix(RFC3339) example
+6.7k Golang : Experimental emojis or emoticons icons programming language