douzi1991
2018-09-08 20:37
浏览 1.2k
已采纳

使用gorm模型的嵌套结构

I have struct called User:

type User struct {
    Email string
    Name string
}

and struct called UserDALModel:

type UserDALModel struct {
    gorm.Model
    models.User
}

gorm Model looks like this:

type Model struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}

this is possible to make UserDALModel nested with gorm model and user model so the output will be:

{
    ID
    CreatedAt
    UpdatedAt
    DeletedAt
    Email 
    Name
}

now the output is:

{
    Model: {
        ID
        CreatedAt
        UpdatedAt
        DeletedAt
    }
    User: {
        Name
        Email
    }
}

图片转代码服务由CSDN问答提供 功能建议

我有一个名为User的结构:

  type User struct {  
电子邮件字符串
名称字符串
} 
   
 
 

和称为UserDALModel的结构:

  type UserDALModel struct  {
 gorm.Model 
模型。用户
} 
   
 
 

gorm模型如下:

  type Model struct {
 ID uint`gorm:“ primary_key”`
 CreatedAt time.Time 
 UpdatedAt time.Time 
 DeletedAt * time.Time`sql:“ index”`
} 
   
 
 

这可以使UserDALModel与gorm模型和用户模型嵌套,因此输出为:

  {
 ID \  n CreatedAt 
 UpdatedAt 
 DeletedAt 
电子邮件
名称
} 
   
 
 

现在输出为:

   {
型号:{
 ID 
 CreatedAt 
 UpdatedAt 
 DeletedAt 
} 
用户:{
 Name 
 Email 
} 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqy27359 2018-09-09 13:46
    已采纳

    I found the answer:

    type UserModel struct {
        Email string
        Name string
    }
    
    type UserDALModel struct {
        gorm.Model
        *UserModal
    }
    
    ------------------------------
    
    user := UserModel{"name", "email@email.com"}
    userDALModel := UserDALModel{}
    userDal.UserModal = &user
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题