Go Course: Methods
Tags: go, tutorial, beginners
Published: 2022-07-20 10:07:57


Let's talk about methods, or 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.

```go
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.

```go
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.

```go
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.

```go
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`.

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

Now, let's run this.

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

	c.UpdateName("Toyota")
	fmt.Println("Car:", c)
}
```

```bash
$ 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.

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

```bash
$ 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.

```go
(&c).UpdateName(...)
```

- We can omit the variable part of the receiver as well if we're not using it.

```go
func (Car) UpdateName(...) {}
```

- Methods are not limited to structs but can also be used with non-struct types as well.

```go
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 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 generally, 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.

---

_This article is part of my open source [Go Course](https://www.karanpratapsingh.com/courses/go) available on Github._

{% github karanpratapsingh/go-course %}