package main import ( "errors" "fmt" ) // Triangle is a struct for modeling triangle shapes type Triangle struct { base, height float64 } // Square is a struct for modeling a square shapes type Square struct { sideLength float64 } type shape interface { getArea() float64 } func (t Triangle) getArea() float64 { return t.base * t.height / 2 } func (s Square) getArea() float64 { return s.sideLength * s.sideLength } func printArea(s shape) { fmt.Println("Area:", s.getArea()) } // NewSquare returns a a new Square struct. func NewSquare(sl float64) (Square, error) { if sl < 0 { s := Square{sideLength: 0} return s, errors.New("square cannot have sides with negative values") } return Square{sideLength: sl}, nil } // NewTriangle returns a new Triangle struct. func NewTriangle(b float64, h float64) (Triangle, error) { if b < 0 || h < 0 { t := Triangle{base: 0, height: 0} return t, errors.New("triangles cannot have negative values") } return Triangle{base: b, height: h}, nil } func main() { s, err := NewSquare(5) if err != nil { fmt.Errorf("unable to create square: %v", err) } t, err := NewTriangle(5, 8) if err != nil { fmt.Errorf("unable to create triangle: %v", err) } printArea(s) printArea(t) }