程序代码如下:
type CheckInStatus struct { ExpTime time.Time LastCheckTime time.Time TransferEnable int64 } ... database.Db. Debug(). Table("users"). Select("expire_time,last_check_in_time,transfer_enable"). Where("id = ?",uid). Scan(&checkInStatus)
扫描出来的结果是
2021/04/04 20:52:49 0001-01-01 00:00:00 +0000 UTC 2021/04/04 20:52:49 0001-01-01 00:00:00 +0000 UTC 2021/04/04 20:52:49 257949696
gorm 成功获取到了数据,但是没有将日期解析为正确的值... 已经在数据库连接 URL 中设置了 &parseTime=True&loc=Local
![]() | 1 mogg 2021-04-04 21:53:37 +08:00 名字对不上,要么在结构体里声明列名,比如 ExpTime time.Time `gorm:"column:expire_time"`,要么 select 的时候用一下 as,Select("expire_time as exp_time …… |
2 toomlo 2021-04-05 17:45:18 +08:00 ```golang type TimeN struct { time.Time } type (t TimeN) String() string{ return t.Format(`2006-01-02 15:04:05`) } type CheckInStatus struct { ExpTime TimeN LastCheckTime TimeN TransferEnable int64 } ``` 如果你需要将 CheckInStatus 这个结构的实例 json 格式化的话 还需要重写 Time 的 MarshalJSON 和 UnmarshalJSON 方法 |
![]() | 3 marcosteam OP @mogg 已经解决,感谢! |