How to type cast a child struct to parent type to pass to a func

15,411 views
Skip to first unread message

prajakta....@gmail.com

unread,
Jun 23, 2015, 1:57:38 AM6/23/15
to golan...@googlegroups.com
I am trying to pass the parent struct type to a method

package main

import "fmt"

type Child struct  {
     Parent  //embed
     Attr string
}

type Parent struct {
    Attr1 string
}

func setf(p Parent) {
fmt.Println(p)   
}


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

    setf(ch) // This does not work - cannot use ch (type Child) as type Parent in argument to setf
}

How do I resolve this problem?

Andrew Gerrand

unread,
Jun 23, 2015, 2:27:07 AM6/23/15
to prajakta....@gmail.com, golang-nuts
Hi,

When you embed the "Parent" struct inside the "Child" struct, the Parent struct is visible as a field named "Parent" of the Child struct.


So in your code, you should be able to write:

  setf(ch.Parent)

Hope this helps,

Andrew

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prajakta....@gmail.com

unread,
Jun 24, 2015, 1:04:40 PM6/24/15
to golan...@googlegroups.com, prajakta....@gmail.com
Hello Andrew

I want to do something else. I want to type cast the Child with Parent. So that I can have a generic method which accepts any object of any of the child structs which are inherited from parent. 

So if I have child1 child2 structs derived from parent. I will write a common method like setf(p Parent)  which can accept any child type object as parameter.

bcgr...@gmail.com

unread,
Jun 24, 2015, 2:24:40 PM6/24/15
to golan...@googlegroups.com, prajakta....@gmail.com
Is the below along the lines you're looking for? playground link 

package main

import "fmt"

type Parent struct {
Attr1 string
}

type Parenter interface {
GetParent() Parent
}

type Child struct {
Parent //embed
Attr   string
}

func (c Child) GetParent() Parent {
return c.Parent
}

func setf(p Parenter) {
fmt.Println(p)
}

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

setf(ch)
}

parais...@gmail.com

unread,
Jun 24, 2015, 3:07:03 PM6/24/15
to golan...@googlegroups.com, prajakta....@gmail.com
There is no typecasting or inheritance in Go. Go doesn't support covariance or contra variance and its type system has no hierarchy for named types.

Write an interface and set "setf" argument type as that interface.It is the only way you'll get some polymorphism out of Go. whatever you are used to in other statically typed languages such as Java doesn't exist with Go type system.


Prajakta Dandawate

unread,
Jun 24, 2015, 3:07:05 PM6/24/15
to bcgr...@gmail.com, golang-nuts
Thank you. Yes I am looking for somewhat similar. I am testing it.

Is this called as Type assertion method

Prajakta Dandawate

unread,
Jun 24, 2015, 3:36:14 PM6/24/15
to bcgr...@gmail.com, golang-nuts
Ok. Thank you All.

So now if I have to add any method for Chile, should be also added to interface or only parent's methods are added to interface?

Haddock

unread,
Jun 25, 2015, 3:17:12 AM6/25/15
to golan...@googlegroups.com, prajakta....@gmail.com

Egon

unread,
Jun 25, 2015, 4:32:46 AM6/25/15
to golan...@googlegroups.com, prajakta....@gmail.com
On Tuesday, 23 June 2015 08:57:38 UTC+3, prajakta....@gmail.com wrote:
I am trying to pass the parent struct type to a method

Can you explain the actual problem, not the facilitated example, you are facing. Maybe there is a simpler/cleaner solution than using Child, Parent. If you can remove the cycle things might get significantly simpler.

+ Egon
Reply all
Reply to author
Forward
0 new messages