Introduction
This challenge aims to test the knowledge of the Go programming language's method feature.
This challenge aims to test the knowledge of the Go programming language's method feature.
The code provided defines a struct type called rect
with two fields, width
and height
. Two methods are defined for this struct type, area
and perim
. The area
method calculates the area of the rectangle, and the perim
method calculates the perimeter of the rectangle. The main function calls these two methods and prints their results.
area
method should have a receiver type of *rect
.perim
method should have a receiver type of rect
.area
method should return the area of the rectangle.perim
method should return the perimeter of the rectangle.main
function should call both methods and print their results.$ go run methods.go
area: 50
perim: 30
area: 50
perim: 30
## Next we'll look at Go's mechanism for grouping and
## naming related sets of methods: interfaces.
This challenge tests the ability to define methods in Go and use them to perform calculations on struct types. The area
and perim
methods are defined for the rect
struct type, and the main
function calls these methods and prints their results.