From 1a51fa131cf5b3dd2d67f0ef14589382e272a744 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Tue, 23 Jun 2015 17:51:05 -0700 Subject: [PATCH] shapes of justice --- src/visitor/Tupfile | 3 +++ src/visitor/main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/visitor/Tupfile create mode 100644 src/visitor/main.go diff --git a/src/visitor/Tupfile b/src/visitor/Tupfile new file mode 100644 index 0000000..4883b55 --- /dev/null +++ b/src/visitor/Tupfile @@ -0,0 +1,3 @@ +include_rules + +: main.go |> !gccgo |> ../../bin/visitor diff --git a/src/visitor/main.go b/src/visitor/main.go new file mode 100644 index 0000000..4b184ca --- /dev/null +++ b/src/visitor/main.go @@ -0,0 +1,43 @@ +package main + +// Shape defines the base for shape functions +type Shape interface { + Area() float64 + Perimeter() float64 +} + +type Square struct { + Shape + Arm float64 +} + +func (s *Square) Area() float64 { + return s.Arm * s.Arm +} + +func (s *Square) Perimeter() float64 { + return s.Arm * 4 +} + +type Triangle struct { + Shape + Base float64 + Height float64 +} + +func (t *Triangle) Area() float64 { + return (t.Base * t.Height) / 2 +} + +func (t *Triangle) Perimeter() float64 { + return 0.0 // I forgot how to do this from just base and height. +} + +func main() { + t := &Triangle{ + Base: 15, + Height: 30, + } + + myT := t.(Shape) +}