Methods in Go! Confusing ? 🤔

While learning Go, I found method is little tricky to understand if a function is already in your mind. I took this concept to understand in and out. After understanding I thought, it's better to explain in a simple language as much as I can for others. If you already understood Method in Go then your suggestions are most welcome. If you trying to get this concept then read on.

Build Up: [ You can skip this if you know concepts from OOP ]

Procedural programming is no more in use after OOP came into the practice. In OOP, we have concepts of classes and objects. Most languages leverage this concept. Go is not an OOP but Go can not be an exception. To attain OOP feature go has a type system. A type system can be called as a little brother to a class. Type is a collection of defined data of same type or different type. Most of the time struct is used as a type system. An instance can be created from struct. This instance is nothing but an object and struct is just like a class. That being said object can be initialized with data which is referred in a type.

➡ What is a Method?

While writing a program, you might face a scenario where some calculations should take place on the same data set. But those calculations are different for the target. For example, you want to find an area of all geometrical shapes like square, rectangle, triangle, etc. So the procedure is the same to find the area but targets are different. Every shape has a side that means input data would have a value of side or sides. So in this case instead of using multiple data structures for each of the shapes, we can use a single data structure and upon that we can find an area of each shape. We could use functions for this as well but if we talk in OOP, this area if the behavior (method) of each shape. So instead of using a function, we can use a new concept called a method which is bounded to the same data structure.

Method: Value Receiver
The syntax for declaring a method:

func (t Type) method_name () {
... method body ...
}

Example: Implementing methods to find out all active employee from a company.

Using a method to find active and inactive employees from a company https://play.golang.org/p/ocsnVJ0YkX0

We can write the same program using a function as well. (check out here https://play.golang.org/p/auHGiwzGqKP)

➡ Why do we need a Method if functions serve the same purpose?
There is a very minute difference between functions and methods.

Example: Go program to demonstrate multiple methods on the same struct.

Data structure struct collects dimensions for square, rectangle, triangle, and circle. Multiple methods are created which calculates area of each geometry using same struct.

Output: 
mdhoke@methods[master !?] ➤ go run area.go
Area of a square of side 4 is: 16
Area of a rectangle of sides 5 7 is: 35
Area of a triangle of base of height 3.5 2.9 is: 5.075
Area of a circle with radius 7.2 is: 162.7776

Method: Pointer Receiver

Syntax:

func (t *Type) method_name(){
:
}

Example:

pointerreceiver.go
Output: 
mdhoke@methods[master !?] ➤ go run pointermethod.go
Area of a square of side 4 is: 16
Old value of side of a square is 4
New value of side of square is 7
Area of square of side 7 is 49

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store