Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Find the Area of a Rectangle Using Classes
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
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: 80Advertisements