Methods in Go! Confusing ? 🤔

Milind Dhoke
4 min readAug 12, 2019

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.

  • In Go, type system can have linked function which can be executed on the instances created from the same type. These functions are nothing but methods. Each method is a behavior of the type.
  • The method can be linked to a type by using receiver which is an argument passed after func keyword and before the method name. Type of receiver will be the name of the type for which method is being created.
  • Methods can be of two types:
    → A
    method with value receiver: All data fields from structs will be made available to the method but field value can not be changed from the struct.
    → A
    method with pointer receiver: All data field from struct will be made available to a method and any value can be changed from the struct.

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.

  • A Method does not take any argument. Method can be invoked by object using dot notation. For function, the object has to be passed as an argument.
  • A most important factor is, a type can have its own operational units which are methods. These methods are tightly coupled with type since the receiver is already declared for that type. Methods use type’s value by default but for function, the value should be provided by a passing object as an argument.
  • Multiple methods can have the same name with different operations. (Kind of polymorphism concept from OOP) but functions can not be with duplicate name.

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

  • Using method with value receiver will do a calculation on the provided data set in a struct. Sometime there might be a need to update a data field from a struct during runtime. Now this change can be achieved using a method with a pointer receiver. Pointer takes references from memory locations.

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

--

--