Methods in Golang

Learn via video courses
Topics Covered

Overview

The methods are supported by Golang. The methods in Golang are similar to that of functions with just one difference, the receiver argument is contained in the method which can access the properties of the receiver argument.

Introduction to Golang Methods

A method is a special type of function that is created by adding a “receiver” between the function and the function name. The receiver is available for access inside the method. Why methods:

  • The method is a way to achieve behavior similar to classes. They can be used with specified types only. Also, they can be invoked using s_v.method()
  • The method is a way to achieve method overloading, a method with the same name with different types.

Golang Methods

In golang, with the help of the receiver argument, the method can access the property of the receiver. The receiver can be of type struct or non-struct. When a method is created in a code, the receiver as well as receiver type, both must be present in the same package. The methods are not allowed to be created in which the type of receiver is defined already. This inthatehaveages like having an inbuilt type, like string, int, etc. the compilation error is thrown when trying to create a method.

Syntax of golang Methods

The syntax of the method is:

Method with struct Type Receiver

In the Go language, if the receiver type is a struct then a method cannot be defined. This is because the receiver is inaccessible inside a method. Let's understand this by using an example:

Go Program to Illustrate the Method with struct Type Receiver

Output:

Explanation: In this example, a struct of type author is made with parameters like name, branch, published articles, and salary. A function is initialized to print the parameters of the author. In the main function, the value of the parameters is assigned to the author. At last, a function show() is called which prints respective values.

Method with non-struct Type Receiver

In the Go language, by using the non-struct type receiver, a method can be created as long as the method definition and type reside in the same package. If the methods are defined in different packages, like int and string, etc, then the compiler pops up the error that the methods are defined in a different package.

Go Program to Illustrate the Method with Non-Struct Type Receiver

Output:

Explanation: In this example, the function multiply takes two int data and returns the output as a multiplication of the two values. In the main function, the two values or the data are initialized and the res variable is assigned with the value that the function call returns. Once a function call is done, the value is printed at the output.

Method with Pointer Receiver along with Method That can Accept Both Value and Pointer

In the Go language, a method can be created by using a pointer receiver. If the pointer receiver is used in a method then changes made in the method get reflected in the caller which is not possible by using other methods.

The syntax for this is:

Go Program to Illustrate Pointer Receiver

Output:

Explanation: In this example, a structure of type author is created with a name, branch, and published articles, all with the type string. In the main function, the variables are initialized with the values that are to be displayed. The pointer receiver method is used for changing the values of the method and to display it.

Method That Can Accept Both Pointers as well as the Value

We know that the function will only accept the value as a parameter when the argument is of a type value. If a pointer is passed to a value function then it won’t accept it and vice versa. But a method in Go language can accept both, a value as well as a pointer because the function type is a value or pointer. This can be understood better by taking an example.

Go Program to Illustrate How the Method Can Accept Pointer and Value

Output:

Explanation: In this example, a function is created that accepts pointer and value. The value of the author is changed by using the pointer in the function.

Advantages of Methods in Golang

The following are the advantages of using methods in the Go language:

  • A method can perform tasks that cannot be performed by a function.
  • They are used for conveying the logic of the code.
  • It also helps implement the interface and is hence used typically.

Difference between Methods and Functions in Golang

MethodFunction
It cannot be used as a first-order object.It can be used and passed as a first-order object.
The method contains receiver.The functions do not contain receiver.
The methods having the same name but different types can be defined in the program.Functions having the same name but different types cannot be defined in the program.

Conclusion

  • The methods are supported by Golang. The methods in Golang are similar to that of functions with just one difference, the receiver argument is contained in the method which can access the properties of the receiver argument.
  • A method is a special type of function that is created by adding a “receiver” between the function and the function name. The receiver is available for access inside the method.
  • Syntax of golang methods. The syntax of the method is:
  • In golang, with the help of the receiver argument, the method can access the property of the receiver. The receiver can be of type struct or non-struct. When a method is created in a code, the receiver as well as receiver type, both must be present in the same package. The methods are not allowed to be created in which the type of receiver is defined already. This includes packages that have an inbuilt type, like string, int, etc. the compilation error is thrown when trying to create a method.