Go methods and receivers

A method is a function associated with a receiver type. The receiver gives the method access to a value or pointer of that type, allowing related behavior to be organized with the data it operates on.

Minimal example

go
type Counter struct {
    Count int
}

func (c *Counter) Increment() {
    c.Count++
}

Learn this concept

Lesson 1.6 · Methods and receivers

Used in exercises

Exercise 1.8 · Practice: write your first Go program Exercise 1.11 · Token bucket limiter with lazy refill Exercise 1.14 · Limiter interface and a FixedWindow implementation Exercise 1.26 · Optional: Sliding window log limiter Exercise 1.27 · Optional: Sliding window counter limiter Exercise 1.28 · Optional: Leaky bucket limiter Exercise 1.30 · Optional: X-RateLimit response headers Exercise 2.22 · GET /v1/restaurants: signed cursors