25% off

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

Get started →

Interactive Go · Lesson

Structs

So far our heroes have been scattered across separate variables. A struct lets you bundle related fields into a single custom type:

type Monster struct {
    Name string
    HP   int
}

You create a value with a struct literal and reach fields with a dot:

goblin := Monster{Name: "Goblin", HP: 30}
fmt.Println(goblin.Name) // Goblin
goblin.HP = 25           // fields are mutable

Your Quest

Create a Hero value named "Elara" at level 5, then print its Name and Level.

Expected output:

Elara 5

Hint

h := Hero{Name: "Elara", Level: 5}, then fmt.Println(h.Name, h.Level).

Additional Scrolls of Wisdom