Created
January 10, 2014 20:44
-
-
Save drewwells/8362221 to your computer and use it in GitHub Desktop.
Inheritance example in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type Car struct { | |
wheelCount int | |
} | |
// define a behavior for Car | |
func (car Car) numberOfWheels() int { | |
return car.wheelCount | |
} | |
type Ferrari struct { | |
Car //anonymous field Car | |
} | |
func main() { | |
f := Ferrari{Car{4}} | |
fmt.Println("A Ferrari has this many wheels: ", f.numberOfWheels()) //no method defined for Ferrari, but we have the same behavior as Car. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment