typeover
curriculum

Curriculum Types & methods Methods exercise 1 · mcq

Methods

Translate the TypeScript method greet on User into the idiomatic Go method declaration. Go's methods are not "inside" the struct like in a TS class. They're top-level functions with a RECEIVER — an extra parameter that goes between func and the method name, written in its own set of parentheses.

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.