You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Go provides a small, powerful set of control flow statements: if/else, switch, for, and defer. Unlike many languages, Go has only one loop construct — the for loop — which covers all looping patterns.
if x > 10 {
fmt.Println("x is greater than 10")
}
Note: no parentheses around the condition, but braces are required.
if x > 10 {
fmt.Println("big")
} else if x > 5 {
fmt.Println("medium")
} else {
fmt.Println("small")
}
Go allows a short statement before the condition — perfect for error handling:
if err := doSomething(); err != nil {
fmt.Println("Error:", err)
return
}
// err is not accessible here — scoped to the if block
This pattern is extremely common in Go. The variable declared in the init statement is scoped to the if/else block.
Go's switch is more powerful than in many other languages:
switch day {
case "Monday":
fmt.Println("Start of the week")
case "Friday":
fmt.Println("Almost weekend")
case "Saturday", "Sunday":
fmt.Println("Weekend!")
default:
fmt.Println("Midweek")
}
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.