This article will cover the details of receiver functions in Go with example programs and their explanations and illustrations.
Overview
Golang allows you to create custom types using type keyword. These custom types may contain Golang inbuilt data types such as a struct or slice as underlying data structures.
For example, there may be an employee type which might use a struct to hold employee data or a deck of cards that may be a slice underneath.

What is receiver function
You can attach functions to these custom types. These functions are called receiver functions and similar to normal functions in Golang with a couple of differences.
1. Syntax
Receiver function definition differs from other functions in that they receive the type to which they are being attached as a variable.
This type is also called receiver and is defined before the function name and after func keyword.
2. Scope
Receiver functions can only be invoked by variables of the type to which they are attached to or the type which they receive.
They can not be called unlike normal functions.
Receiver function syntax
A receiver function is defined using below syntax

func (<variable_name> <type>) function_name(param) return_type {
// code
}

Declaration of a receiver function differs from a normal function in ONLY the part that is highlighted.
In this definition, the highlighted part is called the receiver parameter. So, it could be stated that a receiver function is a a method with receiver parameter.

If you are not familiar with Golang functions, then it is suggested to go through this article first.

Calling receiver function
A receiver function can be invoked using the variable of the type to which it is attached followed by a dot(.) and the name of the function along with parameters, if any as shown in the example below.

Receiver function example
Below code example will clarify the concept of a receiver function.
This example declares a struct using type keyword with two properties. It also defines a receiver function, notice the type between func and function name.

package main

import (
  "fmt"
)

// declare a custom type
type person struct {
  fname string
  lname string
}

func main() {
  // create a struct
  p := person{"ABC", "XYZ"}
  // invoke receiver function
  name := p.getName()
  fmt.Println("Full name is: " + name)
}

// receiver function
func (p person) getName(){
  return p.fname + " " + p.lname
}

This prints

Full name is ABC XYZ

In the above example, notice how a receiver function is defined with the type that it is attached with and how it is invoked using a variable of that type.
Remember that a receiver function has access to all the properties and values of the type that it receives.

Meaning of a receiver function can better be understood with the following image.

Receiver function in golang

Receiver is a copy of type
As stated before, receiver functions receive a copy of the variable on which they are invoked. This means that even if it modifies the value of the variable, it will not be reflected in the original variable or in the calling function.
Example,

package main

import (
  "fmt"
)

type person struct {
  fname string
  lname string
}

func main() {
  p := person{"ABC", "XYZ"}
  p.changeName()
  fmt.Println("First name is: " + p.fname)
}

func (p person) changeName() {
  fmt.Println("Modifiying first name...")
  p.fname = "DEF"
}

Above code defines a receiver function which modifies the value of a field. After the function is invoked, the value of the same field is printed in the calling function.
Look at the output

Modifiying first name…
First name is: ABC

Field value remains unchanged, which shows that the receiver function gets a copy of variable.

Omitting type name
If the receiver function does not make any use of the received data type, then it is legal to omit its name as shown below.

func (person) printMessage(message string) {
   fmt.Println(message)
}

This function simply prints the argument that it receives and so the name of receiver variable is omitted, only its type is written.

That is all on Golang receiver functions. Hope this was helpful.

0
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.