应用场景,GoLang做一套电商系统。
使用了分层,控制层 controller->逻辑层 model ->数据层 storage。
这些层每个层都会定义数据结构,而这些数据结构怎么相互转换?
示例
用户详情列表
接口层 controller 与 logic 之间 dto_user.go
type ReqUserView struct { Id int `json:"id"` UserName string `json:"user_name"` SignAt string `json:"sign_at"` }
逻辑层 logic 数据层 model 之间 do_user.go
type UserDo struct { Id int `json:"id"` UserName string `json:"user_name"` SignAt time.Time `json:"sign_at"` }
数据层 model 与数据库之间 model_user.go
type UserModel struct { Id int `json:"id" gorm:"id"` UserName string `json:"user_name" gorm:"user_name"` SignAt time.Time `json:"sign_at"` }
方案
自己所想的方案:通过JSON。这种方案是普遍方案吗?
但如果JSON数据类型不对,就会报错,除非屏蔽这个JSON.Marshal 转换报错。