go-automapper使用时间.时间字段



我使用go-automapper从数据库字段复制值到body post请求。两个实例的类型相同:

type MessageDTO struct {
CreationDate time.Time          `bson:"creationDate" json:"creationDate,omitempty"`
}

在某个时候,我试图从一个实例复制到另一个实例:

func entityToDTO(entity models.MessageDTO) models.MessageDTO{
dto := &models.MessageDTO{}
automapper.Map(entity, dto)
return *dto
}

但在time.Time值中失败:

错误映射字段:creatationdate。DestType:模型。MessageDTO。SourceType: models.MessageDTO。Error:错误映射字段:wall。DestType: time.Time。SourceType: time.Time。错误:反映:reflect.Value.Set使用使用未导出字段获得的值

有办法让它工作吗?

错误消息中提到失败原因:

Error: Error mapping field: wall…
错误:reflect: reflect. value . set使用未导出字段获取的值

参考time.Time的源代码:

type Time struct {
// wall and ext encode the wall time seconds, wall time nanoseconds,
// ...
wall uint64
ext  int64
// loc specifies the Location that should be used to
// ...
loc *Location
}

go-automapper的文档说明:

未导出/非public的值将不被映射

恐慌是一个设计决策类型中无法映射字段时目的,以确保源或目的地不会导致微妙的沉默错误。

所以我认为没有直接的方法得到它。也许你可以考虑将string或其他类型的时间数据复制到go-automapperMap中,然后使用(t *Time) UnmarshalBinary(t *Time) UnmarshalText等接口转换为time.Time