---
title: "Structs and methods in Go"
slug: "structs-and-methods"
description: "Group fields in a struct, add methods with value and pointer receivers, and see fmt use a String method. Fix a method that changes only a copy."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-go/structs-and-methods"
---

# Structs and methods in Go

How do you group data and behaviour without classes?

## Predict

```go
package main

import "fmt"

type Point struct {
	X, Y int
}

func (p Point) MoveRight() {
	p.X = p.X + 10
}

func main() {
	p := Point{X: 1, Y: 2}
	p.MoveRight()
	fmt.Println(p.X)
}
```

What does this program print?

- 1
- 11
- It does not compile

Answer: 1 — The method has a value receiver, so it changes a copy and the original stays 1.

Output:

```
1
```

A value receiver gets a copy of the struct, so the change is lost when the method returns.

## Structs

A **struct** is a sequence of named fields, each with a type. Declare one with `type Account struct { Owner string; Balance int }`. Make a value with a literal that names the fields: `Account{Owner: "Ada", Balance: 50}`. Read and write a field with a dot: `acc.Owner`. Fields you leave out get their zero values.

## A struct literal and its fields

```go
package main

import "fmt"

type Account struct {
	Owner   string
	Balance int
}

func main() {
	acc := Account{Owner: "Ada", Balance: 50}
	fmt.Printf("%+v\n", acc)
	fmt.Println(acc.Owner)
	acc.Balance = 80
	fmt.Println(acc.Balance)
}
```

Output:

```
{Owner:Ada Balance:50}
Ada
80
```

`%+v` prints the field names with the values, which helps when you debug a struct.

Playground: https://go.dev/play/p/6iEgQhgz2aH

## Methods

A **method** is a function with a receiver written before its name: `func (a Account) String() string`. A value receiver gets a copy of the struct. A **pointer receiver**, `func (a *Account) Deposit(n int)`, gets the address, so it can change the original. A method named `String` is used by `fmt` when it prints the value.

## A pointer method and a String method

```go
package main

import "fmt"

type Account struct {
	Owner   string
	Balance int
}

func (a *Account) Deposit(n int) {
	a.Balance += n
}

func (a Account) String() string {
	return fmt.Sprintf("%s has %d", a.Owner, a.Balance)
}

func main() {
	acc := Account{Owner: "Ada"}
	acc.Deposit(30)
	acc.Deposit(20)
	fmt.Println(acc)
}
```

Output:

```
Ada has 50
```

`acc.Deposit(30)` works on the variable `acc` even though Deposit has a pointer receiver. `Println` calls `String()` for you.

Playground: https://go.dev/play/p/Q2mBQyr8dpI

## Fix it

```go
package main

import "fmt"

type Account struct {
	Owner   string
	Balance int
}

func (a Account) Deposit(n int) {
	a.Balance += n
}

func main() {
	acc := Account{Owner: "Ada"}
	acc.Deposit(30)
	fmt.Println(acc.Balance)
}
```

Deposit runs but the balance stays 0. What is the bug?

- Balance is declared as an int and cannot change
- Deposit has a value receiver, so it adds to a copy of the account
- The method name Deposit is reserved

Answer: Deposit has a value receiver, so it adds to a copy of the account — The method needs a pointer receiver, *Account, to change the original.

Fixed:

```go
package main

import "fmt"

type Account struct {
	Owner   string
	Balance int
}

func (a *Account) Deposit(n int) {
	a.Balance += n
}

func main() {
	acc := Account{Owner: "Ada"}
	acc.Deposit(30)
	fmt.Println(acc.Balance)
}
```

Output:

```
30
```

## Say it back

In your own words: when do you choose a pointer receiver?

Our sentence: Choose a pointer receiver when the method must change the original value; a value receiver works on a copy.

## Mission (8 min)

Build a Counter type with a count field, a pointer method Increment and a String method. Call Increment three times and print the counter.

```sh
go run .
```

## Common questions

### Does Go have classes?

No. Go groups data in struct types and attaches behaviour with methods, which are functions with a receiver. You can define methods on any type you declare yourself. There is no class keyword and no inheritance; instead you build larger types from smaller ones and use interfaces to describe behaviour. For small programs a struct and a few methods cover most of what a class would do.

### When should I use a pointer receiver in Go?

Use a pointer receiver when the method has to change the receiver, because a value receiver only gets a copy. The Go documentation notes that pointer methods can modify the receiver, while value methods work on a copy. Pointer receivers are also common for large structs, since passing an address avoids copying the whole value on every call.

### How do I print a struct in Go?

Pass it to fmt.Println for a plain list of values, or use fmt.Printf with the %+v verb to include the field names, which helps when debugging. If your type has a String method, fmt calls it and prints its result instead. That lets you control exactly how a value of your type appears in output.

## Sources

- https://go.dev/ref/spec#Struct_types (read 2026-12-03)
- https://go.dev/doc/effective_go#pointers_vs_values (read 2026-12-03)
- https://pkg.go.dev/fmt (read 2026-12-03)

Measured with go1.27.4 darwin/arm64 on 2026-12-03.
