shapes-using-methods

command
v0.0.0-...-a2a1f02 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2020 License: MIT Imports: 2 Imported by: 0

README

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

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL