go - range over an []interfaces{} and get the channel field of each type -


i'll try make clear possible, in head first.

i have interface , couple of types inherit declaring method. pretty nice , clever way of inheritance.

i have "super" type thing, other types embed.

the thing struct has size int , out chan properties

what i'm trying understand why can value of size .getsize() both child structs, don't have same success channel field .getchannel() (*ndr i'm using communicate among goroutines , caller)

...here t.getchannel undefined (type measurable has no field or method getchannel)

it might demo of logic:

package main  import (     "fmt" )  type measurable interface {     getsize() int }  type thing struct {     size int     out  chan int } type struct{ *thing } type otherthing struct{ *thing }  func newthing(size int) *thing {     return &thing{         size: size,         out:  make(chan int),     } } func newsomething(size int) *something   { return &something{thing: newthing(size)} } func newotherthing(size int) *otherthing { return &otherthing{thing: newthing(size)} }  func (s *thing) getsize() int         { return s.size } func (s *thing) getchannel() chan int { return s.out }  func main() {      things := []measurable{}      pen := newsomething(7)     paper := newotherthing(5)      things = append(things, pen, paper)      _, t := range things {         fmt.printf("%t %d \n", t, t.getsize())     }      _, t := range things {         fmt.printf("%+v \n", t.getchannel())     }      // _, t := range things {     // fmt.printf("%+v", t.thing.size)     // } } 

the commented code thing i'm trying learn. can value using method declared on super type, not accessing directly child one. sure, resolve type t.(*boththethingtypes).size lose dinamicity, i'm not getting this...

what i'm trying understand why can value of size .getsize() both child structs, don't have same success channel field .getchannel()

type measurable interface {     getsize() int }  ...  things := []measurable{} _, t := range things {     fmt.printf("%+v \n", t.getchannel()) } 

i may missing point seems caused strictly fact measurable interface doesn't have getchannel method.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -