Solution Review: Make a Rectangle
This lesson discusses the solution to the challenge given in the previous lesson.
package mainimport "fmt"type Rectangle struct { // struct of type Rectanglelength, width int}func (r *Rectangle) Area() int { // method calculating area of rectanglereturn r.length * r.width}func (r *Rectangle) Perimeter() int { // method calculating perimeter of rectanglereturn 2* (r.length + r.width)}func main() {r1 := Rectangle{4, 3}fmt.Println("Rectangle is: ", r1)fmt.Println("Rectangle area is: ", r1.Area()) // calling method of areafmt.Println("Rectangle perimeter is: ", r1.Perimeter()) // calling method of perimeter}
Get hands-on with 1200+ tech skills courses.