It's best to have a function to create your BaseAppController: func NewBaseAppContoller(subClasses ...interface{}) *BaseAppController { for _, i := range subClasses { } }
On Monday, 27 June 2016 15:16:40 UTC+1, David Koblas wrote: > > I've got a program that is trying to implement functions on "subclasses", > where the parent can check to see if the interface is implemented. For > perspective, it's really dealing with REST URL generation based on if > methods exist. > > What I'm running into is that based on the following pattern, both the > IList and IGet interfaces are found on the TestController object, when only > 1 is implemented. When the IGet interface is called I get a panic. > > I would rather not make concrete definitions of the Get/List on the base > struct and then have to override them, would much rather do the test for > existence then go from there. > > Code is available on the playground -- > https://play.golang.org/p/5j58fejeJ3 > > package main > > import "fmt" > > type IGet interface { > Get(int) > } > > type IList interface { > List(int) > } > > type Application struct { > name string > } > > type BaseAppController struct { > *Application > > IGet > IList > } > > type TestController struct { > *BaseAppController > } > > func (ctrl *BaseAppController) Init() { > fmt.Println("In Init") > > if f, ok := interface{}(ctrl).(IGet); ok { > fmt.Println("Controller Found GET", f) > } else { > fmt.Println("Controller NOT Found GET", f) > } > > if f, ok := interface{}(ctrl).(IList); ok { > fmt.Println("Controller Found LIST", f) > } else { > fmt.Println("Controller NOT Found LIST", f) > } > } > > func (ctrl *BaseAppController) Call() { > fmt.Println("In Call") > > if f, ok := interface{}(ctrl).(IGet); ok { > fmt.Println("Controller Found GET - going to call", f) > > f.Get(7) > } else { > fmt.Println("Controller NOT Found GET - can't call", f) > } > } > > // Test controller implements the Get Method > func (ctrl *TestController) Get(v int) { > fmt.Printf("Hi name=%s v=%d\n", ctrl.name, v) > } > > func main() { > app := Application{"hithere"} > ctrl := TestController{&BaseAppController{Application: &app}} > > ctrl.Init() > > ctrl.Call() > } > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.