Table of Contents
Are Go Functions and Methods same?
No, I am afraid not. This is one general misconception among many techies.
In Go, a function is a standalone piece of code that can be called by other parts of your program. A method is a function that is associated with a specific type or struct.
The main difference between a function and a method is that a method is associated with a specific type or struct, whereas a function is standalone code that can be called from anywhere in your program. When a method is called, it has access to the fields and properties of the struct it is associated with, whereas a function does not.
Secondly there is some difference in the syntax of method and function, in method the first parameter is always the receiver which is the instance of the struct the method is associated with.
In Go, you can define methods on structs, interfaces, and even on built-in types like int
.
Let's look at some examples
Go Methods vs Function Examples
Example-1
This is a simple function that takes two integers as input and returns their sum. You can see that the function is not associated with any specific type or struct and can be called from anywhere in the program.
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
result := add(2, 3)
fmt.Println(result) // prints 5
}
This is a method that is associated with the MyStruct
struct. The method takes no input and returns the sum of the x
and y
fields of the struct. You can see that the method is defined with a special receiver parameter (s MyStruct)
which is the instance of the struct the method is associated with, and it has access to the fields of the struct.
package main
import "fmt"
type MyStruct struct {
x int
y int
}
func (s MyStruct) add() int {
return s.x + s.y
}
func main() {
myStruct := MyStruct{3, 2}
result := myStruct.add()
fmt.Println(result) // prints 5
}
Example-2
As we explained that one key difference between functions and methods is that methods are associated with a specific type or struct, whereas functions are standalone code that can be called from anywhere in your program. This means that a method has access to the fields and properties of the struct it is associated with, whereas a function does not.
For example, in the following code, the add
method has access to the x
and y
fields of the MyStruct
struct:
package main
import "fmt"
type MyStruct struct {
x int
y int
}
func (s MyStruct) add() int {
return s.x + s.y
}
func main() {
myStruct := MyStruct{3, 2}
result := myStruct.add()
fmt.Println(result) // prints 5
}
While on the other hand, the following function does not have access to the fields of the struct and must be passed the values as arguments:
package main
import "fmt"
type MyStruct struct {
x int
y int
}
func add(a int, b int) int {
return a + b
}
func main() {
myStruct := MyStruct{3, 2}
result := add(myStruct.x, myStruct.y)
fmt.Println(result) // prints 5
}
In this example, the function add
takes two integers as input and returns their sum. It is not associated with any specific type or struct and can be called from anywhere in the program. The function does not have access to the fields of the struct and must be passed the values as arguments.
Example-3
Another important difference is the receiver parameter, which is the first parameter in the method definition. It represents the instance of the struct the method is associated with. This receiver parameter is used to access the fields and properties of the struct from within the method.
In addition to structs, you can also define methods on interfaces and even on built-in types like int
, float64
, string
, etc.
package main
import "fmt"
type MyInterface interface {
add() int
}
type MyStruct struct {
x int
y int
}
func (s MyStruct) add() int {
return s.x + s.y
}
func main() {
var myInterface MyInterface = MyStruct{3, 2}
result := myInterface.add()
fmt.Println(result) // prints 5
}
Example-4
Here's another example that demonstrates the difference between a function and a method in Go:
package main
import "fmt"
type roundShape struct {
radius float64
}
// Method
func (c roundShape) Area() float64 {
return 3.14 * c.radius * c.radius
}
// Function
func Area(c roundShape) float64 {
return 3.14 * c.radius * c.radius
}
func main() {
c := roundShape{5}
fmt.Println("Area of roundShape: ", c.Area()) // prints 78.5
fmt.Println("Area of roundShape: ", Area(c)) // prints 78.5
}
In this example, we have a struct roundShape
with a single field radius
of type float64
. We have defined two ways of calculating the area of a roundShape, one is a method Area()
which is associated with the roundShape
struct, and another one is a function Area(c roundShape)
which is a standalone function.
The method Area()
is defined with a special receiver parameter (c roundShape)
which is the instance of the struct the method is associated with, and it has access to the fields of the struct roundShape
. The function Area(c roundShape)
takes an instance of the struct roundShape
as input and returns the area of the roundShape
.
Both the method and the function have the same logic for calculating the area of a roundShape
and returns the same result. But the method is more readable and easy to understand as it is associated with the struct and it's clear that it is calculating the area of a roundShape
.
Summary
In Go, you can use either functions or methods to accomplish a task, and it depends on the situation and personal preference which one to use. Here is a summary of comparison:
- A function is a standalone piece of code that can be called by other parts of your program.
- A method is a function that is associated with a specific type or struct.
- The main difference between a function and a method is that a method is associated with a specific type or struct, whereas a function is standalone code that can be called from anywhere in your program.
- When a method is called, it has access to the fields and properties of the struct it is associated with, whereas a function does not.
- The syntax of the method is slightly different than the function, in method the first parameter is always the receiver which is the instance of the struct the method is associated with.
- In Go, you can define methods on structs, interfaces, and even on built-in types like int, float64, string, etc.
References
Whats the difference of functions and methods in Go?