我正在尝试保存一个结构数组。
我试过:
type ShiftValue struct {
Hour uint8 `json:"hour"`
Minute uint8 `json:"minute"`
}
type Shift struct {
Start ShiftValue `json:"start"`
End ShiftValue `json:"end"`
}
type Config struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;index;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Shifts []Shift `gorm:"type:varchar(100)[];" json:"shifts,"`
}
但不工作。我还尝试将 Shifts 保存为pq.StringArray:
type Config struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;index;" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at,omitempty"`
Shifts pq.StringArray `gorm:"type:varchar(100)[];" json:"shifts,"`
}
这是一种工作,但我不知道如何将切片转换Shift为StringArray.
我应该使用GenericArrray吗?
我怎样才能从SlicetoGenericArray或进行转换StringArray?
当我Unmarshall获取数据时,我在以下结构中执行它,我验证数据,然后我想将它保存到数据库中:
type ConfigUpdate struct {
Shifts []Shift `json:"shifts,"`
}
繁花如伊
幕布斯7119047
随时随地看视频慕课网APP
相关分类