25% off

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

Get started โ†’

Interactive Go ยท Lesson

If / Else Conditionals

Every adventurer must make decisions. In Go, if runs a block only when its condition is true, and else covers the other case:

if gold >= 100 {
    fmt.Println("You can afford the sword.")
} else {
    fmt.Println("Keep saving, adventurer.")
}

Conditions use comparison operators (==, !=, <, >, <=, >=) and can be combined with && (and), || (or), and ! (not). You can chain checks with else if.

Your Quest

The hero’s health is 30. Print Retreat! if health is below 50, otherwise print Charge!.

Expected output:

Retreat!

Hint

if health < 50 { ... } else { ... } โ€” put a fmt.Println in each branch.

Additional Scrolls of Wisdom