Difference between Function and Methods in Golang

Ravi Kumar Ray
4 min readJan 15, 2023

--

Difference between functions and methods in Golang

The words function and method are used almost interchangeably, but there are subtle differences in their implementation and usage when used in Golang .
Hailing from Java or any other object-oriented language background, the first instinct is to use structs and methods for everything since objects always have a behaviour defined by their methods .

But is that a right approach in Golang where we have functions and methods both ?
Let’s first understand what is a function and what is a method in Golang .

A function takes a few parameters as input and produces some output.
For the same input, the function will always produce the same output.
That means it’s not dependent on the state. Type is always passed as an argument to the function .

Syntax :

Also, two different functions with the same name cannot exist in the same package .

package main

import "math"

type Rectangle struct {
Width float64
Height float64
}

type Circle struct {
Radius float64
}

// `Area` is a function where we're passing the `Rectangle` as an argument
func Area(r Rectangle) float64 {
return 2 * r.Height * r.Width
}

// `Area` is a function where we're passing the `Circle` as an argument
func Area(c Circle) float64 {
return math.Pi * c.Radius * c.Radius
}

The above code throws an error : Area redeclared in this block

A method on the other hand in Go is a function that is attached to a specific receiver(type) argument (Example : struct). It defines the behaviour of the type and it should use the state of the type . The output of a method is dependent on the type of the receiver . Methods are also known as receiver functions. Here, the receiver can be of struct type or non-struct type .

Syntax :

Different methods with the same name with a different receiver are allowed in Golang .

package main

import (
"fmt"
"math"
)

type Rectangle struct {
Width float64
Height float64
}

// The `Rectangle` type is the receiver of the `Area` method
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}

type Circle struct {
Radius float64
}

// The `Circle` type is the receiver of the `Area` method
func (c Circle) Area() float64 {
return math.Pi * c.Radius * c.Radius
}

type Triangle struct {
Base float64
Height float64
}

// The `Triangle` type is the receiver of the `Area` method
func (t Triangle) Area() float64 {
return 0.5 * t.Base * t.Height
}

func main() {
r := Rectangle{
Width: 10.0,
Height: 10.0,
}
fmt.Printf("Area of rectangle %#v is = %g \n", r, r.Area())
c := Circle{
Radius: 10,
}
fmt.Printf("Area of Circle %#v is = %g \n", c, c.Area())
t := Triangle{
Base: 12,
Height: 6,
}
fmt.Printf("Area of triangle %#v is = %g", t, t.Area())
}

In Go, you are also allowed to create a method with a pointer receiver.
With the help of a pointer receiver, if any change is made in the method, it will reflect in the caller which is not possible with the value receiver methods .

package main

import (
"fmt"
"math"
)

type Vertex struct {
X, Y float64
}

func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

// The `Vertex` pointer type is the receiver of the `Scale` method
func (v *Vertex) Scale(f float64) {
v.X = v.X * f
v.Y = v.Y * f
}

func main() {
v := Vertex{3, 4}
v.Scale(10)
fmt.Println(v.Abs())
}

Conclusion

Although we went over some of the key differences and use cases for functions and methods in Go, there are always some exceptions!

In the end, the difference between functions and methods is in how the resulting code reads. If you or your team feel that one way reads better than the other, then that’s the correct abstraction. It’s important not to take any of these rules as set-in-stone.

--

--

Ravi Kumar Ray

I'm a software engineer with 8.5+ years of experience. I have been working with Java, Go & many other languages & technologies. I love to learn & teach .