I have the following code which primarily marshals and un-marshals a time struct. Here is the code
package main
import (
"fmt"
"time"
"encoding/json"
)
type check struct{
A time.Time `json:"a"`
}
func main(){
ds := check{A:time.Now().Truncate(0)}
fmt.Println(ds)
dd, _ := json.Marshal(ds)
d2 := check {}
json.Unmarshal(dd, d2)
fmt.Println(d2)
}
here is the output it produces
{2019-05-20 15:20:16.247914 +0530 IST}
{0001-01-01 00:00:00 +0000 UTC}
The first line is the original time and the second line is the time after the unmarshalling. Why do we have this loss of information with JSON
conversions? How to prevent this?
Thanks.