mapstructure:",squash"这个标签怎么用啊

看介绍是把对应的struct直接提到本级来,比如:

type ShoppingCart struct {
    BaseModel `mapstructure:",squash"`
    User      int32 `gorm:"type:int;index"`
    Goods     int32 `gorm:"type:int;index"`
    Nums      int32 `gorm:"type:int"`
    Checked   bool
}
type BaseModel struct {
    ID        int32     `gorm:"primarykey;type:bigint"`
    CreatedAt time.Time `gorm:"column:add_time"`
    UpdatedAt time.Time `gorm:"column:upd_time"`
    DeletedAt gorm.DeletedAt
    IsDeleted bool
}

在使用的时候就可以ShoppingCart{ID:1} 这样用了,我看别人框架里这么用的没有问题,但是我自己这么写就无法直接使用ID等字段,用了就报错,是不是还有哪些细节需要处理?

讨论数量: 7

你这个是 mapstructure 这个包解析结构体数据到map时候用到的,不是赋值时用的

1年前 评论
deatil (作者) 1年前
meitian (楼主) 1年前

Embedded_Structs_and_Squashing 看下这个文档

1年前 评论
meitian (楼主) 1年前
snowlyg (作者) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!