44 lines
615 B
Go
44 lines
615 B
Go
|
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)
|
||
|
}
|