Hello,

I have a parent/child class and a method that accepts parameters as below. 
How do I type cast to parent/child in setf and access its elements.


t := reflect.ValueOf(p).Elem().Type() returns types as Parenter for setf




type Parenter interface {
    GetParent() interface{}
}

type Parent struct {
    Attr1 string
}

func (p Parent) GetParent() interface{} {
    return p
}

type Child struct {
    Parent //embed
    Attr2   string
}

func (c Child) GetParent() interface{} {
    return c
}


type Child2 struct {
    Parent //embed
    Attr3   string
}

func (c Child) GetParent() interface{} {
    return c
}

func setf(p Parenter) {
    if p is child {
      //access child elements - Attr2
    }
    if p is child2 {
      //access child2 elements - Attr3
    }}

func main() {
    var ch Child
    ch.Attr1 = "1"
    ch.Attr2 = "2"
    setf(ch)


    var ch2 Child2
    ch2.Attr1 = "1"
    ch2.Attr2 = "2"
    setf(ch2)

    var p Parent
    p.Attr1 = "2"
    setf(p)
}






-- 
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.

Reply via email to