github.com/wfusion/gofusion@v1.1.14/test/common/di/cases/types.go (about) 1 package cases 2 3 import ( 4 "context" 5 6 "github.com/wfusion/gofusion/common/di" 7 "github.com/wfusion/gofusion/log" 8 ) 9 10 type Eat[T any] interface { 11 Egg(T) 12 } 13 14 type Drink interface { 15 Water() 16 } 17 18 type PersonEat[T any] struct { 19 } 20 21 func NewPersonEat[T any]() Eat[T] { 22 return new(PersonEat[T]) 23 } 24 25 func (p *PersonEat[T]) Egg(something T) { 26 log.Info(context.TODO(), "鸡蛋好好吃 %+v", something) 27 } 28 29 type PersonDrink struct { 30 eating Eat[int] 31 } 32 33 func NewPersonDrink(eating Eat[int]) Drink { 34 return &PersonDrink{eating: eating} 35 } 36 func (p *PersonDrink) Water() { 37 p.eating.Egg(3) 38 log.Info(context.TODO(), "要多喝水") 39 } 40 41 type Person struct { 42 di.In 43 44 Eating Eat[int] 45 Drinking Drink 46 } 47 48 func (p *Person) Show() { 49 p.Eating.Egg(2) 50 p.Drinking.Water() 51 } 52 53 type Person2 struct { 54 di.In 55 56 Eating Eat[string] 57 Drinking Drink 58 } 59 60 func (p *Person2) Show() { 61 p.Eating.Egg("string") 62 p.Drinking.Water() 63 } 64 65 type Person3 struct { 66 di.In 67 68 Eating Eat[string] 69 Drinking Drink `name:"ddd"` 70 } 71 72 func (p *Person3) Show() { 73 p.Eating.Egg("string") 74 p.Drinking.Water() 75 }