drpaneas
(Panos Georgiadis)
March 18, 2020, 11:51am
1
I have two structs which they both have the same methods:
func (r *myStruct1) DoThis() error {}
func (r *myStruct1) DoThat() error {}
func (r *myStruct2) DoThis() error {}
func (r *myStruct2) DoThat() error {}
I don’t like the duplication here, since they are actually both doing the same thing, just for different structs. Is there any way to generalise them? If so, could you perhaps come up with a code example?
ermanimer
(erman imer)
March 18, 2020, 12:44pm
2
This is called composite design pattern.
package main
import "fmt"
type myStruct1 struct {
method func()
}
type myStruct2 struct {
method func()
}
func doThis() {
fmt.Println("test")
}
func main() {
struct1 := myStruct1{
method: doThis,
}
struct2 := myStruct1{
method: doThis,
}
struct1.method()
struct2.method()
}
1 Like
system
(system)
Closed
June 16, 2020, 12:44pm
3
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.