Methods in Go

Contents
Receivers
Value vs pointer receivers
Fluent setters
The Stringer interface

Receivers

A method is a function with a receiver: the extra parameter in front of the name that binds the function to a type. Callers then use the familiar dot form, as in r.Area instead of a plain function call.

package main import "fmt" type Rect struct { W int H int } func (r Rect) Area() int { return r.W * r.H } func main() { r := Rect{W: 3, H: 4} fmt.Println(r.Area()) }

go run main.go

12

Inside the method the receiver behaves like an ordinary parameter, so r.W reads the width of whoever called Area. Methods keep behavior next to the data it works on.

Value vs pointer receivers

Value receivers work on a copy and suit small read only operations, while pointer receivers share the original and are required for any method that mutates it. As a rule of thumb, use pointer receivers for mutation and value receivers for pure computation.

package main import "fmt" type Counter struct { Value int } func (c *Counter) Inc() { c.Value++ } func (c Counter) Get() int { return c.Value } func main() { c := Counter{Value: 0} c.Inc() fmt.Println(c.Get()) }

1

Go takes the address for you: calling Inc on the variable c works even though the receiver wants *Counter. Stay consistent within one type and avoid mixing receiver styles without a reason.

Fluent setters

A setter that returns its own receiver lets calls chain into one fluent line: each step configures the value and hands it to the next step. The pattern shines in builders and configuration helpers.

package main import "fmt" type Email struct { To string Body string } func (e *Email) SetTo(to string) *Email { e.To = to return e } func (e *Email) SetBody(body string) *Email { e.Body = body return e } func main() { e := &Email{} e.SetTo("ada@example.com").SetBody("Hello") fmt.Println(e.To, e.Body) }

ada@example.com Hello

Each method returns the same pointer it received, so the chain keeps configuring one shared value. Stop chaining when steps need error checks: plain sequential calls with if statements read better there.

The Stringer interface

The fmt package prints any value whose type defines String returning a string by calling that method automatically. Implementing this one method, known as the Stringer interface, controls how custom types appear in logs and output.

package main import "fmt" type Point struct { X int Y int } func (p Point) String() string { return fmt.Sprintf("(%d, %d)", p.X, p.Y) } func main() { fmt.Println(Point{X: 1, Y: 2}) }

(1, 2)

Without String the same call would print the raw {1 2} form. The next lesson on interfaces explains how one method can satisfy a shared contract across many types.

Next: Interfaces

Article author: Arthur Isaev

Related articles
Golang
Go course: zero to hero in 50 lessons
What is Go and where it runs
Install Go and check the version
Your first Go program
Modules with go mod
Variables
Constants
Types
Numbers
Strings
Conditions with if
Switch in Go
Loops with for
Arrays in Go
Slices in Go
Maps in Go
Functions in Go
Errors as values
Pointers in Go
Structs in Go
Methods in Go
Interfaces in Go
Embedding instead of inheritance
Generics in Go
Packages and imports
The go toolchain
Testing with go test
Advanced errors
defer in Go
Strings, bytes and runes
Time in Go
JSON
Files and IO
HTTP servers
HTTP clients
Context
Goroutines
Channels
select and sync
CLI: args, flags, env
Logging
Regular expressions
Sorting
Concurrency patterns
Databases with database/sql
Benchmarks and profiling
Toolchain and CI
Project layout
Capstone: wordfreq CLI
Capstone: JSON API server
Hero roadmap: the whole course on one page
Install Go on Windows 11
Install Go on Ubuntu
Install Go on Rocky Linux
Install Go on macOS
Install Go on FreeBSD
Go in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: