25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →

Interactive Go · Lesson

Methods

A method is a function attached to a type via a receiver in front of its name:

func (m Monster) Describe() string {
    return m.Name + " lurks nearby"
}
goblin.Describe()

The receiver can be a value (m Monster) or a pointer (m *Monster). Use a pointer receiver when the method needs to modify the struct — a value receiver works on a copy, so changes wouldn’t stick.

func (m *Monster) TakeHit(dmg int) {
    m.HP -= dmg   // mutates the original
}

Your Quest

Add a method Heal with a pointer receiver (*Hero) that adds 10 to the hero’s HP.

Expected output:

100

Hint

func (h *Hero) Heal() { h.HP += 10 } — the pointer receiver lets it change the real hero, not a copy.

Additional Scrolls of Wisdom