Go structs and interfaces
Go doesn’t have a class
to define custom types. Instead, you would use a struct
.
type MotorVehicle struct {
wheelSize int
modelName string
manafacturer string
}
Then, to add method to this type, we would just implement methods against this type as described on our previous post.
func (mv MotorVehicle) startEngine(){
fmt.Printf("Starting %s's engine\n", mv.modelName)
}
Multiple inheritance can be achieved by type embedding.
type Car struct {
MotorVehicle
//otherFields
}
When type embedding, the fields (and methods of the parent type) can be referenced without sub-referencing the embedded type (assuming you don’t overload that same field names). This of course only works when adding an anonymous field (not giving the embedded type a name) to the struct. So, using the above example, we could do:
var mazdaCx9 Car
mazdaCx9.wheelSize = 17
//or
mazdaCx9.MotorVehicle.wheelSize = 17
..
To implement an interface is go, you firstly define the interface as a type (similar to how a struct is defined). Whereas struct’s can only house fields, interfaces can only house function specifications.
type PoweredVehicle interface {
startEngine()
}
So, with that, any type that implements the method startEngine, becomes a PoweredVehicle. Then we can pass any derived types as a PoweredVehicle type to call any supporting methods.
func goToShops(pv PoweredVehicle){
pv.startEngine()
//further implementation
}
myCar := MotorVehicle{}
myCar.wheelSize = 11
myCar.modelName = "abc"
myCar.manafacturer = "def"
goToShops(myCar)
Further reading: