pointer receiver
A method receiver declared as a pointer, such as (c *Counter), which lets the method change the original value rather than a copy.
In more detail
Use one when a method modifies state, as Add does with c.Value++, or when the struct is large. On an addressable variable Go takes the address for you, so c.Add() works without writing &c.
Example
func (c *Counter) Add() {
c.Value++
}Related terms
- method receiver — The special first parameter of a Go method, written before the method name, that is the value the method is called on.
- pointer — A value that stores the memory location of another piece of data, so the program can follow it to that data.
- addressable value — A value in memory that Go can take the address of with &, such as a variable, a struct field or a slice element.
- method — A function that belongs to an object or type and is called on a value of it, as in name.upper().
Taught in
- Structs and Methods — Concurrent Systems Programming with Go