Structs and methods in Go
Learn Go · Beginner · 11 min · After Functions, multiple returns and errors in Go
How do you group data and behaviour without classes?
1Guess
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) }2Read
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.3Code
A struct literal and its fields
1 / 2package 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) }Read the program. What will it print?
4Read
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 namedStringis used byfmtwhen it prints the value.5Code
A pointer method and a String method
1 / 2package 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) }Read the program. What will it print?
6Fix
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) }7Say it back
8Mission · 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.
go run .
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
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
Published Dec 3, 2026 · Updated Dec 3, 2026