Solution Review: Advancing the Shapes Analysis
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport "fmt"type Square struct {side float32}type Triangle struct {base float32height float32}type AreaInterface interface {Area() float32}type PeriInterface interface {Perimeter() float32}func main() {var areaIntf AreaInterfacevar periIntf PeriInterfacesq1 := new(Square)sq1.side = 5tr1 := new(Triangle)tr1.base = 3tr1.height = 5areaIntf = sq1fmt.Printf("The square has area: %f\n", areaIntf.Area())periIntf = sq1fmt.Printf("The square has perimeter: %f\n", periIntf.Perimeter())areaIntf = tr1fmt.Printf("The triangle has area: %f\n", areaIntf.Area())}func (sq *Square) Area() float32 {return sq.side * sq.side}func (sq *Square) Perimeter() float32 {return 4 * sq.side}func (tr *Triangle) Area() float32 {return 0.5 * tr.base*tr.height}
Get hands-on with 1400+ tech skills courses.