Problem overview
The function legalAge takes an integer age and returns a bool: true when the person is 18 or older, false otherwise. For 17 it returns false; for 18 and for 40 it returns true.
The point of the lesson is a code-quality habit, a way of writing the same logic with less code. Many beginners write this function with an if and an else. This lesson shows that the if isn't needed.
Approach: the comparison is already the answer
The long version looks like this:
func legalAge(age int) bool {
if age >= 18 {
return true
} else {
return false
}
}
Read it carefully: when the condition is true, it returns true; when the condition is false, it returns false. The function returns exactly what the condition evaluates to. A comparison such as age >= 18 is an expression whose value is a bool, and a function can return any expression of the right type. So the whole body shrinks to one line.
The same rule works for any condition. If you ever write if cond { return true } else { return false }, replace it with return cond. For the reverse case, returning false when the condition holds, return its negation: return !cond.
Step by step
- Return the comparison. Write
return age >= 18. The operator>=includes 18 itself, which is right because someone who is exactly 18 counts as of legal age. The expression has typebool, which matches the function's result type.
The solution in Go
func legalAge(age int) bool {
return age >= 18
}
Details that matter in Go:
- The result type decides what you can return. The signature says
bool, and a comparison produces abool, so no conversion is needed. Go has no truthy values, so returning the numberageor a string wouldn't compile. - Precedence. In
return age >= 18the comparison is evaluated first and its result is whatreturnsends back. No parentheses are required. - Short is easier to read. The one-line form is less code to scan and has fewer places for mistakes, such as returning the same value from both branches.
- Combined conditions work the same way. A function can return
age >= 13 && age <= 19directly. Another lesson in this chapter does exactly that for a range of ages.
Reusing the result
A function that returns a bool can sit directly in the condition of another if, which is where the short form pays off:
func isMinor(age int) bool {
return !legalAge(age)
}
if legalAge(20) {
fmt.Println("adult")
}
Here isMinor(17) is true and isMinor(18) is false, and the if prints adult. Writing return age < 18 gives identical answers, and for a single comparison that is the simpler choice. Both forms avoid the three-line if that only repeats the condition's own value. The habit applies beyond ages: whenever a function's job is to answer a yes-or-no question, look for the expression that already holds the answer, and return it as it is.
Edge cases and common mistakes
- The boundary.
legalAge(18)must returntrue. Using>instead of>=would returnfalse. - Comparing with
true. Writingif legalAge(age) == truein calling code is the same redundancy in a different place. Useif legalAge(age). - Negative ages. The function doesn't validate its input, so
legalAge(-5)returnsfalse. For this lesson that is the expected result. - Writing a ternary. Go has no
? :operator. Returning the comparison is the idiomatic replacement for the boolean case, and anif/elseis the replacement when the two results are not booleans.
Testing
legalAge(17)returnsfalse.legalAge(18)returnstrue.legalAge(40)returnstrue.legalAge(0)returnsfalse.