By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A bool holds exactly two values: true and false. That sounds small, and it is enough to express every decision a program makes. A program that checks a budget, validates an expense or chooses a message is comparing values and acting on the answer.
You rarely type true yourself. Booleans come from comparisons: == (equal), != (not equal), <, <=, > and >=. Each comparison is an expression that produces a bool, so you can print it, store it in a variable, or hand it straight to an if.
package main
import "fmt"
func main() {
total := 1250
limit := 1000
fmt.Println("over limit:", total > limit)
fmt.Println("equal:", total == limit)
fmt.Println("different:", total != limit)
fmt.Println("under or equal:", total <= limit)
}Notice that = stores a value and == compares two values. Mixing them up is the most common beginner mistake in this lesson. In Go the mistake is caught for you: a condition must be a bool, so if total = 1250 is a compile error and if total == 1250 is the question you meant. There is no truthy value in Go either: if count is an error, and if count > 0 is what you wanted to say.
Three operators combine booleans. && is and: the result is true only when both sides are true. || is or: the result is true when at least one side is true. ! is not: it flips a boolean. && binds more tightly than ||, so a || b && c means a || (b && c). Do not make a reader work that out; add parentheses when you combine three or more conditions.
An if statement runs its block only when the condition is true. The block is the group of statements between the braces. When the condition is false, Go skips the block and continues after it.
When there are more than two outcomes, chain the tests with else if. Go checks each condition from top to bottom and runs the first block whose condition is true. The remaining conditions are not evaluated at all, which is why the order of the branches changes the result.
package main
import "fmt"
func main() {
total := 1250
if total >= 5000 {
fmt.Println("large month")
} else if total >= 1000 {
fmt.Println("normal month")
} else {
fmt.Println("quiet month")
}
fmt.Println("report finished")
}Order matters. If the first test had been total >= 1000, it would have caught every large amount too, and the later branches would never run. Put the narrowest or most urgent test first.
A guard clause checks for the bad case first and leaves. The rest of the program can then assume the good case, so the main path stays flat and readable instead of hiding inside one if after another. return leaves the function immediately; inside main it ends the program.
package main
import "fmt"
func main() {
cents := 1250
paid := true
if cents < 0 {
fmt.Println("negative expense")
return
}
if !paid {
fmt.Println("not paid")
return
}
fmt.Println("expense is fine")
if cents > 1000 && paid {
fmt.Println("that is a big one")
}
}Braces create a block, and a variable declared inside a block exists only until the closing brace. You can use it inside the block, and the compiler reports undefined if you try to use it afterwards. This rule is called scope, and it keeps names from leaking across the program.
package main
import "fmt"
func main() {
total := 1250
if total > 1000 {
size := "large"
fmt.Println("size:", size)
}
fmt.Println("total:", total)
}Write a program that decides whether a Penny expense is over budget. Use budget := 2000 and cents := 1875. Print under budget when cents is less than or equal to budget, and over budget otherwise. Add a second decision: when cents is greater than 1000 and the expense name is "lunch", print big lunch. Then set cents to -50 and add a guard that prints invalid amount and returns before the rest of the program runs.
Key takeaways
A bool holds true or false, usually produced by a comparison.
== compares and = assigns; the condition in an if must be a bool.
&&, || and ! combine conditions; use parentheses when three or more are involved.
Go runs the first true branch of an if / else if chain and skips the rest.
Guard clauses leave early, and braces create a block where a declared variable lives.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.