Golang Program to Find the Area of a Rectangle Using Classes

Go ProgrammingServer Side ProgrammingProgramming

To find the area of a rectangle using classes, we can take following Steps

  • Define a struct with rectangle properties such as breadth and length.
  • Define a method to calculate the area of the rectangle.
  • In the main method, instantiate an object of rectangle.
  • Call the struct method, i.e., Area, to calculate the area of the rectangle.
  • Print the area of the rectangle.

Example

 Live Demo

package main
import (
   "fmt"
)
type Rectangle struct {
   breadth int
   len int
}
func (r *Rectangle)Area() int{
   return r. len * r.breadth
}
func main(){
   rectangle := Rectangle{
      breadth: 10,
      len: 8,
   }
   fmt.Println("Area of the rectangle:", rectangle, " is: ",
   rectangle.Area())
}

Output

Area of the rectangle: {10 8} is: 80
raja
Published on 30-Jul-2021 15:15:58
Advertisements