rectangle.go 492 B

1234567891011121314151617181920212223242526272829
  1. // rectangle.go
  2. package main
  3. import "fmt"
  4. type Rectangle struct {
  5. length, width int
  6. }
  7. func (r *Rectangle) Area() int {
  8. return r.length * r.width
  9. }
  10. func (r *Rectangle) Perimeter() int {
  11. return 2 * (r.length + r.width)
  12. }
  13. func main() {
  14. r1 := Rectangle{4, 3}
  15. fmt.Println("Rectangle is: ", r1)
  16. fmt.Println("Rectangle area is: ", r1.Area())
  17. fmt.Println("Rectangle perimeter is: ", r1.Perimeter())
  18. }
  19. /* Output:
  20. Rectangle is: {4 3}
  21. Rectangle area is: 12
  22. Rectangle perimeter is: 14
  23. */