Ad space (banner)
๐ŸนGo Lessons
Lesson 4 / 68

Conditionals (if statements)

In this lesson you will learn how to write if statements in Go so your program can branch based on a condition. This is for anyone searching for "Go if statement syntax".

Go's if statement needs no parentheses () around the condition. else if adds further conditions. It helps to picture it as translating an everyday decision such as "if it is raining, take an umbrella" into code.

The sample code prints one of three messages depending on score using if / else if / else. Notice the simple Go rule at work: you may drop the parentheses, but you may never drop the braces {}.

A common stumbling block for beginners is adding parentheses out of habit from another language while forgetting the braces. Comparisons use operators such as >, <, and ==, and stacking more else if clauses naturally expresses three or more branches.

In real-world development if statements appear in almost every program, from validating input to switching behaviour based on state.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)