typeover
curriculum

Curriculum Types & methods Methods exercise 2 · mcq

Methods

Go methods can take their receiver by VALUE (func (c Counter) bump()) or by POINTER (func (c *Counter) bump()). The choice isn't cosmetic — it determines whether the method can MUTATE the caller's struct. The TypeScript bump below mutates this.n. Pick the Go receiver shape that ACTUALLY mutates the caller's Counter (not a copy).

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.