Golang implements interfaces as types and as collection of methods(specs for what should be considered of that type). Let’s first talk about collection of methods (specs that other types could implement)
So what we did, is define a blueprint for Vehicle type, any type would be considered a vehicle that implements all methods listed in vehicle.
Let’s start defining some vehicles
Okay, so we defined three structs that implements out Vehicle interface. Now lets looks it in action
SO here we creted a slice of type Vehicle and stuffed our structs implementing Vehicle, ieterating over them easily. If we assign type that does not implements, methods in Vehicle interface it can not be included in slice, let’s look at it
So this clearly not going to accept Jumbo in Vehicle slice as it does not implement Vehicle
Now we come to the next use of interface, that is type. In function’s argument signature can accept an interface type, meaning it could accept any type of value as those can be converted/casted to interface
That’s interface. <3. Please pass your opinion/comments to @idealogylabs at twitter