A square can't have different width and height

This commit is contained in:
Mark McGranaghan
2015-04-19 08:26:23 -07:00
parent 518a807773
commit 8b66bf7fcb
3 changed files with 23 additions and 23 deletions

View File

@@ -13,8 +13,8 @@ type geometry interface {
}
// For our example we'll implement this interface on
// `square` and `circle` types.
type square struct {
// `rect` and `circle` types.
type rect struct {
width, height float64
}
type circle struct {
@@ -23,12 +23,12 @@ type circle struct {
// To implement an interface in Go, we just need to
// implement all the methods in the interface. Here we
// implement `geometry` on `square`s.
func (s square) area() float64 {
return s.width * s.height
// implement `geometry` on `rect`s.
func (r rect) area() float64 {
return r.width * r.height
}
func (s square) perim() float64 {
return 2*s.width + 2*s.height
func (r rect) perim() float64 {
return 2*r.width + 2*r.height
}
// The implementation for `circle`s.
@@ -50,13 +50,13 @@ func measure(g geometry) {
}
func main() {
s := square{width: 3, height: 4}
r := rect{width: 3, height: 4}
c := circle{radius: 5}
// The `circle` and `square` struct types both
// The `circle` and `rect` struct types both
// implement the `geometry` interface so we can use
// instances of
// these structs as arguments to `measure`.
measure(s)
measure(r)
measure(c)
}

View File

@@ -1,2 +1,2 @@
9bafbade1f5effc38be233c3fd12cbdd7c6e226d
guxTzcucR-
3547b935d1e0322c0fb696726c27cae53a275e0a
313UebA3rD