Golang:将子结构类型转换为父结构时,子结构信息会丢失吗?

For example after embedding the parent struct in the child struct:

type ParentNode struct {
}

type ChildNode struct {
    ParentNode
    Ident string
}

func ParentType() ParentNode { 
    child := ChildNode{Ident : "node"}
    fmt.Println(child)
    return child.ParentNode 
}

func main() {
    x := ParentType()
    fmt.Println(x.Ident)

}

Would this print out "node" and also return the child struct wrapped in the parent struct with all the information, such that we can manipulate the apparent parent struct while having the actual child struct? The idea for this is similar to Java where you can return an apparent type of List but return an actual type of LinkedList.

If not, what would be the best way to achieve this functionality? Essentially I want to upcast the Child struct to a parent struct but manipulate it as if it was a child struct. Is there a way of solving this using an interface?

How is it possible to get rid of error "x.Ident undefined (type ParentNode has no field or method Ident)" at line fmt.Println(x.Ident)

It would not return the child struct. What you are doing is not typecasting the child struct, but just returning one of its fields. To use a more concrete example, the above go snippet would be like trying to use the name of an Employee as if it were a super class of Employee.

Your guess was right, though, that the way to achieve the "is-a" relationship like ArrayList and List is to use an interface. Know, though, that interfaces can only provide polymorphism for method calls, not field access. A modified version of your example, which is hopefully helpful, can be found at:

http://play.golang.org/p/qclS5KR64H

You would likely find it helpful to read the "Struct Types" portion of the go spec, and/or the whole spec (it's not that long! Or scary!):

https://golang.org/ref/spec#Struct_types