Methods vs Functions , Understanding how to use receivers in Go
Imagine a scenario Where we have a struct type with some fields and we want to define methods that can access and modify those fields.
Defining a method & function in Go is as follows:
func (receiverName ReceiverType) MethodName(parameters) (returnType) {
// Method body
}
func functionName(parameterName parameterType) returnType {
// Function body
}
To achieve this
1) Define a method for the struct type that can access and modify its fields and
2) Define a method with a receiver parameter that refers to the instance of the struct type. Use the receiver parameter to access and modify the fields of the struct.
We can now use the method to access and modify the fields of the struct type.
type Rectangle struct {
length float64
width float64
}
func (r Rectangle) Area() float64 {
return r.length * r.width
}
func main() {
r := Rectangle{length: 10, width: 5}
area := r.Area()
fmt.Println("Area of rectangle:", area)
}
In this example, we define a Rectangle struct with length and width fields. We define a method called Area that calculates and returns the area of the rectangle.
The method has a receiver parameter of type Rectangle, which allows it to access the fields of the struct.
In the main function, we create a new instance of the Rectangle struct and call the Area method on it. The method uses the values of the length and width fields of the struct to calculate the area and returns it, which we then print to the console.
Methods vs Functions
In Go, both methods and functions are used to define reusable pieces of code, but they have some key differences in terms of their usage, syntax, and behavior. Here are some of the main differences between methods and functions in Go:
Receiver parameter: The most significant difference between methods and functions in Go is that methods are associated with a receiver parameter, which allows them to access the fields and methods of the receiver type. Functions, on the other hand, do not have a receiver parameter and are standalone entities.
Scope: Methods are defined within a struct type and have access to the fields and methods of that type. Functions, on the other hand, are not defined within any specific type and can be called from anywhere in the program.
Syntax: The syntax for defining methods and functions is slightly different. In Go, methods are defined using the
func
keyword followed by the receiver type and the method name. Functions, on the other hand, are defined using only thefunc
keyword followed by the function name and parameters.Mutability: Methods can modify the state of the receiver type by changing its fields or calling its methods. Functions, on the other hand, cannot modify the state of any type and must rely on their input parameters and return values.
type Rectangle struct {
length float64
width float64
}
// This is a method
func (r Rectangle) Area() float64 {
return r.length * r.width
}
// This is a function
func CalculateArea(length, width float64) float64 {
return length * width
}
func main() {
r := Rectangle{length: 10, width: 5}
area1 := r.Area() // Using a method
area2 := CalculateArea(10, 5) // Using a function
fmt.Println("Area of rectangle (using method):", area1)
fmt.Println("Area of rectangle (using function):", area2)
}
In this example, we define a Rectangle struct and a method called Area that calculates the area of the rectangle using its length and width fields. We also define a function called CalculateArea that calculates the area of a rectangle using input parameters.
In the main function, we create a new instance of the Rectangle struct and call the Area method on it to calculate its area. We also call the CalculateArea function with the same length and width values to calculate the area of a rectangle. Finally, we print the results to the console.
I hope this clarifies the differences between methods and functions in Go.