I got a interface with more than one method. I wonder how to write a middleware for it.
I seek in Google but found all the answers are for interface with only one method. I found nothing for my problem. And I try to write a demo, but it does not work.
package main
import (
"fmt"
"strconv"
)
type ti interface {
Say(int) string
Eat(string) int
}
type Middleware func(ti) ti
func GetMiddleWare(t ti) ti {
var tm ti
t.Say = func(i int) string {
fmt.Println("arg is " + strconv.Itoa(i))
var ret string
defer func() {
fmt.Println("ret is " + ret)
}()
ret = t.Say(i)
return ret
}
t.Eat = func(s string) int {
fmt.Println("arg is " + s)
var ret int
defer func() {
fmt.Println("ret is " + strconv.Itoa(ret))
}()
ret = t.Eat(s)
return ret
}
return tm
}
it does not work
.\main.go:17:8: cannot assign to t.Say
.\main.go:26:8: cannot assign to t.Eat
So, how can I write a middleware for an interface with more than one method?