-1
我有一个封装的方法:如何通过接口间接golang
func Route(router *mux.Router){
subrouter := router.PathPrefix(_API).Subrouter()
subrouter.Path(_FOO).HandlerFunc(foo)
subrouter.Path(_BAR).HandlerFunc(bar)
}
,我想通过在我的包的匹配接口,简单涵盖所有功能以除去多路复用器的外部的依赖上面使用,就像这样:
type Router interface{
Path(string) Path
PathPrefix(string) Path
}
type Path interface{
HandlerFunc(http.HandlerFunc)
Subrouter() Router
}
func Route(router Router){
subrouter := router.PathPrefix(_API).Subrouter()
subrouter.Path(_FOO).HandlerFunc(foo)
subrouter.Path(_BAR).HandlerFunc(bar)
}
但是当我建立这个我得到错误:
*mux.Router does not implement api.Router (wrong type for Path method) have Path(string) *mux.Route want Path(string) api.Path
,但我认为接口隐式在golang中使用,所以我认为*mux.Route
确实实现了我的Path接口。