我正在尝试保存结构数组。我尝试过:
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
吗?如何进行从Slice
到GenericArray
或StringArray
的转换?
[当我Unmarshall
数据时,我按以下结构进行操作,我验证了数据,然后将其保存到数据库中:
type ConfigUpdate struct {
Shifts []Shift `json:"shifts,"`
}
package gorm_test
import "testing"
type BasePost struct {
Id int64
Title string
URL string
}
type Author struct {
ID string
Name string
Email string
}
type HNPost struct {
BasePost
Author `gorm:"embedded_prefix:user_"` // Embedded struct
Upvotes int32
}
type EngadgetPost struct {
BasePost BasePost `gorm:"embedded"`
Author Author `gorm:"embedded;embedded_prefix:author_"` // Embedded struct
ImageUrl string
}