Get field time in database

I have a problem retrieving data in the database. why insert with different output results at created_at & updated_at?

insert : 2019-03-23 00:00:00
output : “0001-01-01T00:00:00Z”

with the source code as follows:

Struct :
type Builder struct {
Id int json:"id"
Name string json:"name"
Created_at time.Time json:"created_at"
Updated_at time.Time json:"updated_at"
}

Database :
CREATE TABLE test_types (
id bigint NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE now(),
PRIMARY KEY (id)
);

INSERT INTO test_types (id, name, created_at) VALUES (1, ‘Welcome Screen’, ‘2019-03-23 00:00:00’);

Output :
{
“id”: 1,
“name”: “Welcome Screen”,
“created_at”: “0001-01-01T00:00:00Z”,
“updated_at”: “0001-01-01T00:00:00Z”
},

GO : go1.11.1
Database : Mariadb 10.3.13

Is there something wrong with my code? thank you.

To properly work with the time and mysql driver you should use parseTime parameter in connection string. Another observation is about using timestamp field which is not quite a very good idea. If you work with only one timezone can be ok but for multiple timezones use datetime or other equivalent and let the client to add his particular timezone. On short, use UTC on the server.

1 Like