Values & References12 / 15
Up next

Pointer receiver mutates

package main

import "fmt"

type Counter struct{ n int }

// pointer receiver: mutates the caller's value
func (c *Counter) Inc() {
	c.n++
}

func main() {
	c := Counter{}
	c.Inc()
	c.Inc()
	fmt.Println(c.n) // 2
}
What you're solving

*T receiver changes the original