How to call a parent method and pass any child that extends the parent structure as an argument in the Golang

I am still studying the Golang, and I want to ask something. Is it possible to do something like this and pass on to any other child in PMethod that extension of the parent structure?

type Parent struct{
   PAttribute string
}

func (p *Parent) PMethod(c *Child){
   fmt.Println("this is parent Attribute : " + p.PAttribute)
   fmt.Println("this is child Attribute : " + c.CAttribute)
}

type Child struct{
   Parent
   CAttribute string
}

type Child2 struct{
   Parent
   CAttribute string
}


func main(){
   c := Child{
         Parent{
            "parent"
         },
         "child",
        }
   c.PMethod(&c)

   c2 := Child2{
         Parent{
            "parent"
         },
         "child",
        }
   c2.PMethod(&c2)
}

thank

-3
source share
1 answer

, , Go . . Go ( ), -. interfaces Go, Child and Child2, , ( ). , , , , Go. .

, , , , . - , . , , /, .

. - . Animal. , , , , Say() Animal, Say(), woofs Say(), .

Go Say() , Speaker(), - . , .

" " , , , , .

+1

Source: https://habr.com/ru/post/1690576/


All Articles