登录
首页 >  Golang >  Go问答

拥有许多实现的许多部分

来源:stackoverflow

时间:2024-04-03 22:57:33 323浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《拥有许多实现的许多部分》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我需要注册一组实现相同接口的实体,但它们必须注册在不同的表中,因为它们具有不同的结构。

我正在寻找与 jpa 实现的行为类似的行为: hibernate继承映射

以下示例经过简化。

type Person struct {
    ID         uint64 `gorm:"primarykey"`
    Name       string
    Identifier []identificable
}

type identificable interface {
    GetValue() string
}

type DNI struct {
    ID       uint64 `gorm:"primarykey"`
    Value    string
    PersonID uint64
}

func (dni DNI) GetValue() string {
    return dni.Value
}

type DrivingLicense struct {
    ID       uint64 `gorm:"primarykey"`
    Value    string
    PersonID uint64
}

func (licence DrivingLicense) GetValue() string {
    return licence.Value
}

func main() {
    db := getDB()
    dni1 := DNI{}
    dni1.Value = "testdni1"
    drivingLicense1 := DrivingLicense{}
    dni1.Value = "License1"

    person := Person{}
    person.Name = "test_name"
    person.Identifier = []identificable{dni1, drivingLicense1}

    result := db.Create(&person)
    if result.Error != nil {
        fmt.Sprintf(result.Error.Error())
    }
}

正确答案


GORM 从结构名称中派生出表名称,与接口无关。

每个结构都会放入自己的表中。

请提供更多代码和详细信息,说明您到底在哪里遇到了问题。

今天关于《拥有许多实现的许多部分》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>