Rectangle Calculator
A struct with methods for area, perimeter, and square detection.
package main
import "fmt"
type Rectangle struct {
Width float64
Height float64
}
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
func (r Rectangle) Perimeter() float64 {
return 2 * (r.Width + r.Height)
}
func (r Rectangle) IsSquare() bool {
return r.Width == r.Height
}
func (r Rectangle) Display() {
fmt.Printf("%.1f x %.1f (area: %.1f, perimeter: %.1f, square: %t)\n",
r.Width, r.Height, r.Area(), r.Perimeter(), r.IsSquare())
}
func main() {
shapes := []Rectangle{
{Width: 10, Height: 5},
{Width: 7, Height: 7},
{Width: 3, Height: 12},
}
for _, s := range shapes {
s.Display()
}
}