Assignment from base class to inherit class in Go

630 views
Skip to first unread message

Tong Sun

unread,
Jul 27, 2015, 8:51:31 PM7/27/15
to golang-nuts
I know Go don't have such OO concept, but let me borrow them just to make it easier to explain. 

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,

Thanks

Ref:


Roberto Zanotto

unread,
Jul 27, 2015, 9:03:32 PM7/27/15
to golang-nuts, sunto...@gmail.com
You can do it like this http://play.golang.org/p/iUuuyxY9Yy
You should initialize all fields or none. If you want just some, you have to use the notation c = Car{Vehicle: v}.

For struct literals the following rules apply:

  • A key must be a field name declared in the LiteralType.
  • An element list that does not contain any keys must list an element for each struct field in the order in which the fields are declared.
  • If any element has a key, every element must have a key.
  • An element list that contains keys does not need to have an element for each struct field. Omitted fields get the zero value for that field.
  • A literal may omit the element list; such a literal evaluates to the zero value for its type.
  • It is an error to specify an element for a non-exported field of a struct belonging to a different package
Also try not to write java code in Go :)

Roberto Zanotto

unread,
Jul 27, 2015, 9:10:46 PM7/27/15
to golang-nuts, sunto...@gmail.com
I just saw that you linked the article "inheritance and subclassing in go". Just don't do that and don't read that. Use Go like an improved C. You need to implement an algorithm, you need some data structure... use structs and functions. Don't start thinking about Cars and Vehicles and class hierarchies. If you want a function to operate on different data types, maybe an interface can be appropriate.
Cheers.


On Tuesday, July 28, 2015 at 2:51:31 AM UTC+2, Tong Sun wrote:

Tong Sun

unread,
Jul 27, 2015, 9:15:20 PM7/27/15
to Roberto Zanotto, golang-nuts
Thanks a lot for all the explanations. 

Roberto Zanotto

unread,
Jul 27, 2015, 9:37:29 PM7/27/15
to golang-nuts, sunto...@gmail.com
You are welcome. Just to be clear, there are situations where anonymous fields are useful. For example, if you have a type A which implements an interface F, you can define the type B with:
type B struct {
   A
   
...
}
And B will "magically" implement interface F.
What I meant is that if you have a problem to solve in Go, you should think about structs and functions and interfaces, inheritance should be the last thing on your mind.

Have fun writing Go, I guarantee you won't miss classes.

Tong Sun

unread,
Jul 28, 2015, 12:14:37 AM7/28/15
to Roberto Zanotto, golang-nuts

On Mon, Jul 27, 2015 at 9:37 PM, Roberto Zanotto <roby...@gmail.com> wrote:
What I meant is that if you have a problem to solve in Go, you should think about structs and functions and interfaces, inheritance should be the last thing on your mind.

yeah, sure. Understand. 

I'm only doing variable assignment, to give my `var Opts` (in https://github.com/suntong001/ffcvt/blob/master/config.go) a good set of initial values (from a struct variable of Encoding, when different -t types are specified). I think it is a good use case for anonymous field. After all, I do want my Go program to be like Go program. :-)


Reply all
Reply to author
Forward
0 new messages