Skip to content

Commit

Permalink
Example of O-O in Go using embedded struct and interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy Hinners committed Feb 27, 2017
1 parent ade6641 commit f714177
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions carvalue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
)

type CarValue interface {
value() float64
}

type Car struct {
Make string
Year int
}

func (c *Car) value() float64 {
switch {
case c.Year >= 2000 && c.Year < 2010:
return 6500
case c.Year >= 2010 && c.Year < 2020:
return 9000
default:
return 3000
}
}

func NewCar(make string, year int) *Car {
return &Car{make, year}
}

type Tesla struct {
Car
}

func (t *Tesla) value() float64 {
switch {
case t.Year >= 2010 && t.Year < 2015:
return 18000
default:
return 24000
}
}

func NewTesla(year int) *Tesla {
return &Tesla{*NewCar("Tesla", year)}
}

func value(c CarValue) float64 {
return c.value()
}

func main() {
herbie := NewCar("Volkswagen", 1963)
model3 := NewTesla(2017)
fmt.Println("Herbie is worth", value(herbie))
fmt.Println("Model3 is worth", value(model3))
}

0 comments on commit f714177

Please sign in to comment.