Working version
This commit is contained in:
74
app/inheritance/inheritance.go
Normal file
74
app/inheritance/inheritance.go
Normal file
@ -0,0 +1,74 @@
|
||||
package inheritance
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Model struct{}
|
||||
|
||||
type MyModel struct {
|
||||
Model
|
||||
}
|
||||
|
||||
type MethodInitInterface interface {
|
||||
Init(dbConn int)
|
||||
}
|
||||
|
||||
type RepoBase[T interface{}] struct {
|
||||
DbConn int
|
||||
GetMethod[T]
|
||||
ListMethod[T]
|
||||
methods []MethodInitInterface
|
||||
}
|
||||
|
||||
func (b *RepoBase[T]) InitMethods(dbConn int) {
|
||||
for _, method := range b.methods {
|
||||
method.Init(dbConn)
|
||||
}
|
||||
}
|
||||
|
||||
type CRUDRepo[T interface{}] struct {
|
||||
RepoBase[T]
|
||||
SaveMethod[T]
|
||||
}
|
||||
|
||||
func (m *CRUDRepo[T]) Init(dbConn int) {
|
||||
m.methods = []MethodInitInterface{&m.GetMethod, &m.ListMethod, &m.SaveMethod}
|
||||
m.InitMethods(dbConn)
|
||||
}
|
||||
|
||||
func DoInheritanceTest() {
|
||||
repo := RepoBase[MyModel]{
|
||||
DbConn: 111,
|
||||
// GetMethod: GetMethod{
|
||||
// DbConn: 666,
|
||||
// },
|
||||
// ListMethod: ListMethod{
|
||||
// DbConn: 777,
|
||||
// },
|
||||
}
|
||||
repo.GetMethod.Init(888)
|
||||
repo.ListMethod.Init(888)
|
||||
|
||||
repo.GetMethod.Get()
|
||||
repo.List()
|
||||
|
||||
fmt.Printf("outside Base: %d\n", repo.DbConn)
|
||||
fmt.Printf("outside GetMethod: %d\n", repo.GetMethod.DbConn)
|
||||
fmt.Printf("outside ListMethod: %d\n", repo.ListMethod.DbConn)
|
||||
|
||||
fmt.Println("----------------")
|
||||
|
||||
crudRepo := CRUDRepo[MyModel]{}
|
||||
crudRepo.Init(999)
|
||||
|
||||
crudRepo.Get()
|
||||
crudRepo.List()
|
||||
crudRepo.Save()
|
||||
|
||||
fmt.Printf("outside GetMethod: %d\n", crudRepo.GetMethod.DbConn)
|
||||
fmt.Printf("outside ListMethod: %d\n", crudRepo.ListMethod.DbConn)
|
||||
fmt.Printf("outside SaveMethod: %d\n", crudRepo.SaveMethod.DbConn)
|
||||
|
||||
// repo.DbConn = 123
|
||||
// repo.SomeGetVar = 456
|
||||
// repo.DoSomething()
|
||||
}
|
||||
43
app/inheritance/methods.go
Normal file
43
app/inheritance/methods.go
Normal file
@ -0,0 +1,43 @@
|
||||
package inheritance
|
||||
|
||||
import "fmt"
|
||||
|
||||
type GetMethod[T interface{}] struct {
|
||||
SomeGetVar int
|
||||
DbConn int
|
||||
}
|
||||
|
||||
func (m *GetMethod[T]) Init(dbConn int) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m GetMethod[T]) Get() T {
|
||||
var model T
|
||||
fmt.Printf("Get DbConn: %d\n", m.DbConn)
|
||||
return model
|
||||
}
|
||||
|
||||
type ListMethod[T interface{}] struct {
|
||||
SomeListVar int
|
||||
DbConn int
|
||||
}
|
||||
|
||||
func (m *ListMethod[T]) Init(dbConn int) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ListMethod[T]) List() {
|
||||
fmt.Printf("List DbConn: %d\n", m.DbConn)
|
||||
}
|
||||
|
||||
type SaveMethod[T interface{}] struct {
|
||||
DbConn int
|
||||
}
|
||||
|
||||
func (m *SaveMethod[T]) Init(dbConn int) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m SaveMethod[T]) Save() {
|
||||
fmt.Printf("List DbConn: %d\n", m.DbConn)
|
||||
}
|
||||
Reference in New Issue
Block a user