Structs and Methods

A struct groups fields into one box. A method is a function attached to that box.

The idea

A struct is a form with labelled blanks (Name, Age, ...). A method is an action you can perform on a filled-in form, like form.Submit().

The key question for every method is: does it get the form, or a photocopy? A value receiver (c Counter) gets a photocopy, so changes vanish. A pointer receiver (c *Counter) gets the address of the real form, so changes stick.

Watch it work

Try it

Common mistakes