Methods in GoLang

Methods are just like functions, except it has a special argument and that is a receiver. In this post, we will dive deeper into GoLang methods.

GoLang Methods Syntax

A method consists of the func keyword, the receiver argument, and the function body. Here is the syntax of a Go method.

func (receiver receiverType)funcName(arg argType) returnType {}

So, when we want to call the function we simply do receiver.funcName(arg).

This allows Go to do the OOP-like method call.

What is a receiver?

A receiver is essentially just a type that can directly call the function. Here is an example of a struct type having a method. Here the struct Bird is the receiver. That means any object of type Bird can call the function Fly. The receiver’s declaration should be in the same package that of the method otherwise, it won’t work.

package main

import (
	"fmt"
)

type Bird struct{
	name string
}

// declare method
func (b Bird)Fly() {
	fmt.Println(b.name, "is flying...")
}

func main() {
	b := Bird{"Raven"}
	
	// call method
	b.Fly()            // Raven is flying...
}

Why use receivers?

Receivers allow us to write function calls in an OOP manner. That means whenever an object of some type is created that type can call the function from itself.

package main

import (
	"fmt"
)

type Person struct{
	name string
}

func (p Person)Name(){
	fmt.Println(p.name)
}

func main() {
	// create object
	p := Person{"Jack"}
	
	// call method
	p.Name()         // Jack
}

Receiver types

There are two types of receivers that are available in Go. The value receivers and the pointer receivers. Here is an example showing both of them.

package main

import (
	"fmt"
)
type Animal struct{
	name string
}

func (a Animal)Run(){
	fmt.Println(a.name, "is running...")
}

func (a *Animal)RunFaster(){
	fmt.Println(a.name, "is running...")
}

func main() {
	a := Animal{"Lion"}
	a.Run()                // Lion is running...
	a.RunFaster()          // Lion is running...
}

As can be seen, both of the functions do the same thing even if they are declared differently.

Methods on structs

Methods can be defined by structs. It is really useful to do so since structs are the closest thing to a class in Go. They allow data encapsulation and with the methods described they will behave as a class does in an OOP language. Here is an example showing methods on a struct.

package main

import (
	"fmt"
)

type Human struct{
	name string
	age int
}

func (h Human)Describe() {
	fmt.Println(h.name, "is", h.age, "years old.")
}

func main() {
	h := Human{"John", 23}
	h.Describe()             // John is 23 years old.
}

GoLang Methods benefits

Methods have some benefits over regular functions. In the same package functions with the same name are not allowed but the same is not true for a method. One can have multiple methods with the same name given that the receivers they have are different.

If we have two structs but they call the same methods then it is possible in Go. This is one of the most important aspects of methods which makes it behave like an OOP.