typeover
curriculum

Curriculum Types & methods Methods exercise 3 · mcq

Methods

Given a method declared with a POINTER receiver: ``go func (c *Counter) bump() { c.n++ } ` How do you CALL bump on a regular Counter value c`? Pick the form that works — Go's auto-addressing rules let you call pointer-receiver methods through a value, but only when the value is addressable.

TypeScript reference
Pick the idiomatic Go translation

About this theme

Methods are functions with a receiver — a special argument declared before the function name: func (r Receiver) Foo() { ... }. Two flavours: value receivers (operate on a copy) and pointer receivers (operate on the original). The rule of thumb: use a pointer receiver if you'd modify the receiver, or if the receiver is large enough that copying is wasteful.