Methods

Pointer receiver methods

A pointer receiver allows mutation of the pointed-to value. The method set of *T includes methods declared on T and *T for ordinary defined base types. A nil receiver can be handled by code, but dereferencing nil panics. Example output: '3'.

Formal Syntax & Structure

go syntaxgo
func (t *T) Method()

Working Example

Example Snippetgo
package main
import "fmt"
type Counter struct{ N int }
func (c *Counter) Inc() { c.N++ }func main() {  c := Counter{N: 2}  c.Inc()  fmt.Println(c.N)}
Go Reference