I have a question in Go especially with gin-gionic and gorm.
Let's say I have model like this
// Classroom struct.
type Classroom struct {
gorm.Model
Name string `json:"name"`
Code string `json:"code"`
StartedAt time.Time `json:"started_at"`
}
I want to create data of Classroom
Model with this JSON
{
"name": "Math",
"code": "math-mr-robie",
"started_at": "2020-10-10 10:00:00"
}
But when I bind the JSON data, I got this following error
parsing time ""2020-10-10 10:00:00"" as ""2006-01-02T15:04:05Z07:00"": cannot parse " 10:00:00"" as "T"
I know that error appear because of the format that I sent was not the exact format of time.Time
?
Is it possible to set default format of time.Time
?
How to do that?
Because I've try to add .Format in after time.Time
but error occurs.
// Classroom struct.
type Classroom struct {
gorm.Model
Name string `json:"name"`
Code string `json:"code"`
StartedAt time.Time.Format("2006-01-02 15:04:05") `json:"started_at"`
}
I resolve this issue by creating new struct
JSONData
that contain time inside it.After I red Customize Data Types in Gorm and see some examples here then I add some methods
For the gin things. Another resource @Eklavya given. So I add another methods.
And it's works!