Skip to content

Instantly share code, notes, and snippets.

@drewwells
Created January 10, 2014 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewwells/8362221 to your computer and use it in GitHub Desktop.
Save drewwells/8362221 to your computer and use it in GitHub Desktop.
Inheritance example in Go
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