How to .Scan() a MySQL TIMESTAMP value into a time.Time variable?
I know this is an old question however I was missing this parameter in my Open call:
parseTime=true
See here
I've found the cause of the error.
Since rating
is NULL in the database, the scanner gave the error
sql: Scan error on column index 3: converting string "nil" to a uint8: strconv.ParseUint: parsing "nil": invalid syntax
I've updated the database row and now usr.date_registered
and usr.online
hold the correct values.
I guess I'll have to make the MySQL field NOT NULL and just use -1 to indicate a non-initialised value.
In addition to answer by @incognick, here is exactly you can do (add parseTime=true):
db, err := sqlx.Connect("mysql", "myuser:mypass@tcp(127.0.0.1:3306)/mydb?parseTime=true")
If there is a possibility of timestamp/datetime be null, in that case you should use scan parameter as sql.NullTime instead of time.Time.
Using sql.NullTime will provide you option to check if scanned time is null or not using var.Valid
flag. You can use time with var.Time
if it is valid and not null.