Module 01 Β· Go Basics β Lesson 3 of 3 Β· ~15 min
Control flow
if, with an optional head start
Go's if looks familiar β but no parentheses around the condition, and the braces are required even for one line. The bonus: if can run a short init statement first, separated by a semicolon:
// price is in scope here (and in the else) }
That declares price, then tests it. price lives only inside the if/else β a tidy way to scope a value to exactly where it's used. You'll see this constantly with the err pattern: if err := doThing(); err != nil { ... }.