Ensuring embedded structs implement interface without introducing ambiguity

前端 未结 2 964
情话喂你
情话喂你 2020-12-06 03:54

I\'m trying to clean up my code base by doing a better job defining interfaces and using embedded structs to reuse functionality. In my case I have many entity types that c

2条回答
  •  忘掉有多难
    2020-12-06 04:29

    Seems to me having three ID in one structure with methods relying on them is even semantically incorrect. To not be ambiguous you should write some more code to my mind. For example something like this

    type Baz struct {
        EntityModel
        Foo []*Foo
        Bar []*Bar
    }
    func (b Baz) LinkFoo() {
        (&FooLinkerEntity{b.EntityModel, b.Foo}).LinkFoo()
    }
    func (b Baz) LinkBar() {
        (&BarLinkerEntity{b.EntityModel, b.Bar}).LinkBar()
    }
    

提交回复
热议问题