I'm trying to learn Go, but I keep bashing my head against some of its concepts that apply differently compared to other languages.
Suppose I have a struct
type Vehicle struct {
Seats int
}
I now want another struct, which embeds Vehicle:
type Car struct {
Vehicle
Color string
}
As I understand it, the Car struct now embeds Vehicle.
Now I want to have a function that takes any vehicle
func getSeats(v Vehicle){
return v.Seats
}
But whenever I try to pass a Car:
getSeats(myCar)
I get the following error:
cannot use myCar (value of type Car) as Vehicle value in argument to getSeats
But my IDE tells me myCar has a Seats property!
From this I understand that embedding is not the same as inheriting.
My question is; Is there an equivalent to struct inheritance like c++, where a function can take a base struct, or is this something Go handles completely different? How would I implement something like this in the "Go way"?