Methods

4 min read

Authors
banner

Let's talk about methods, sometimes also known as function receivers.

Technically, Go is not an object-oriented programming language. It doesn't have classes, objects, and inheritance.

However, Go has types. And, you can define methods on types.

A method is nothing but a function with a special receiver argument. Let's see how we can declare methods.

func (variable T) Name(params) (returnTypes) {}

The receiver argument has a name and a type. It appears between the func keyword and the method name.

For example, let's define a Car struct.

type Car struct {
	Name string
	Year int
}

Now, let us define a method like IsLatest which will tell us if a car was manufactured within the last 5 years.

func (c Car) IsLatest() bool {
	return c.Year >= 2017
}

As you can see, we can access the instance of Car using the receiver variable c. I like to think of it as this keyword from the object-oriented world.

Now we should be able to call this method after we initialize our struct, just like we do with classes in other languages.

func main() {
	c := Car{"Tesla", 2021}

	fmt.Println("IsLatest", c.IsLatest())
}

Methods with Pointer receivers

All the examples that we saw previously had a value receiver.

With a value receiver, the method operates on a copy of the value passed to it. Therefore, any modifications done to the receiver inside the methods are not visible to the caller.

For example, let's make another method called UpdateName which will update the name of the Car.

func (c Car) UpdateName(name string) {
	c.Name = name
}

Now, let's run this.

func main() {
	c := Car{"Tesla", 2021}

	c.UpdateName("Toyota")
	fmt.Println("Car:", c)
}
$ go run main.go
Car: {Tesla 2021}

Seems like the name wasn't updated, so now let's switch our receiver to pointer type and try again.

func (c *Car) UpdateName(name string) {
	c.Name = name
}
$ go run main.go
Car: {Toyota 2021}

As expected, methods with pointer receivers can modify the value to which the receiver points. Such modifications are visible to the caller of the method as well.

Properties

Let's also see some properties of the methods!

  • Go is smart enough to interpret our function call correctly, and hence, pointer receiver method calls are just syntactic sugar provided by Go for convenience.
(&c).UpdateName(...)
  • We can omit the variable part of the receiver as well if we're not using it.
func (Car) UpdateName(...) {}
  • Methods are not limited to structs but can also be used with non-struct types as well.
package main

import "fmt"

type MyInt int

func (i MyInt) isGreater(value int) bool {
	return i > MyInt(value)
}

func main() {
	i := MyInt(10)

	fmt.Println(i.isGreater(5))
}

Why methods instead of functions?

So the question is, why use methods instead of functions?

As always, there's no particular answer for this, and in no way one is better than the other. Instead, they should be used appropriately when the situation arrives.

One thing I can think of right now is that methods can help us avoid naming conflicts.

Since a method is tied to a particular type, we can have the same method names for multiple receivers.

But in the end, it might just come down to preference, such as "method calls are much easier to read and understand than function calls" or the other way around.

© 2024 Karan Pratap Singh