shapes-using-methods example
Calculating the area and perimeter of circles, rectangles and triangles
using methods.
This example is done using,
GitHub Webpage
OVERVIEW
This is pretty straight forward using methods.
It is very similar to using functions except we're using a
function receiver type. Refer to code.
As you can see its similar to a function,
// Get the shape properties
c1Area := c1.circleArea()
c1Perimeter := c1.circlePerimeter()
r1Area := r1.rectangleArea()
r1Perimeter := r1.rectanglePerimeter()
t1Area := t1.triangleArea()
t1Perimeter := t1.trianglePerimeter()
You could argue there is no advantage and you'll be right.
But with interfaces, you can reduce the function name to
just area() and parameter(). See interface example.
RUN
go run shapes-using-methods.go