Methods

Value receiver methods

A value receiver receives a copy of the receiver value. Reassigning its fields does not change the caller struct, although mutation through shared reference-like fields may be visible. Example output: '2'.

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