I know Go doesn't have such OO concept, but let me borrow them just to make it easier to explain.
OO inherits allows generalizing behavior to a more abstract type or class, using a class and a subclass of it, where the subclass inherits all the member fields and behaviors of the parent class. Go doesn't have such concept, but can achieve the same paradigm, in which the article explains Inheritance and subclassing in Go.
My question is more on the assignment. Suppose I have a "base class" Vehicle and a "inherit class" Car, like the following:
type Vehicle struct {
wheelCount int
}
type Car struct {
Vehicle //anonymous field Vehicle
Maker string
}
In true OO, I can assign a Vehicle object into a Car object, but I haven't found a way to do that in Go. So my questions are,
- Is it possible to do that?
- Else, what's the Go way to write a "Car" constructor from a "Vehicle"? I know the canonical way is
c := Car{Vehicle{4}, "Ford"}
, but what if I have other member fields in Car, like Module etc, and I'd like only to initial the member fields from Vehicle, nothing else?
I've prepared something for you to start with, http://play.golang.org/p/dcs9r7vPjZ
Thanks