Problem overview
The function isNegative takes one integer, n, and prints a word. If n is less than zero it prints negative. In every other case it prints positive. The function prints; it doesn't return anything, so its signature has no result type.
Three examples show the rule. n = 5 prints positive. n = -2 prints negative. The interesting one is n = 0: zero is not less than zero, so it prints positive, even though in everyday speech zero is neither positive nor negative. The lesson treats "positive" as "zero or above".
Approach
There are exactly two outcomes, and one comparison separates them: n < 0. When it is true, print negative. When it is false, print positive. That is what an if/else does, so no other tool is needed. The choice is made once, in constant time.
Step by step
- Write the
ifwith its condition. The test isn < 0. Go takes no parentheses around it, and the body goes in braces. - Print
negativeinside the block withfmt.Println("negative").Printlnends the output with a line break, which is what the lesson's tests expect. - Add the
elsebranch and printpositivein it. It needs no condition: it runs for everynthat is not below zero, which includes zero. Write it as} else {on one line.
The solution in Go
func isNegative(n int) {
if n < 0 {
fmt.Println("negative")
} else {
fmt.Println("positive")
}
}
Some Go details behind these few lines:
- Printing, not returning.
fmt.Printlnwrites to the console. The function's caller gets nothing back, which is why the signature ends with)and no type. - Strict versus non-strict comparison.
n < 0is false for zero. Writingn <= 0would send zero to the wrong branch. - The strings need double quotes. In Go, single quotes make a one-character rune, so
'negative'is an error. Text always uses double quotes (or backticks). - The parameter type.
n intmeans the function accepts whole numbers only. Passing a value such as2.5is a compile error, because Go won't convert it to anintfor you. On this platform anintis 64 bits wide, so the range is far larger than any test needs. - Exactly one branch runs. Each call prints one line, never two and never none.
Another way to write it
Go programmers often leave out the else when the first branch ends the function. A bare return in a function with no result type simply exits it:
func isNegative(n int) {
if n < 0 {
fmt.Println("negative")
return
}
fmt.Println("positive")
}
It behaves the same for every input; calling it with 5, 0, -2 and -1 prints positive, positive, negative, negative. The lesson uses the explicit else, which makes the two outcomes visible side by side. That is the clearer form while you are learning, and either one is fine in real code.
Edge cases and common mistakes
- Zero.
isNegative(0)printspositive. - Using
<=. It makes zero printnegative, which breaks Example 2. - Using
returninstead of printing. The function has no result type, soreturn "negative"doesn't compile, and the tests look at what is printed. - Misspelled output. The text must be exactly
negativeorpositive, in lowercase. - The largest and smallest integers. A single comparison never overflows, so the function works for every
intvalue, unlike code that negates the number first.
Testing
isNegative(5)printspositive.isNegative(0)printspositive.isNegative(-2)printsnegative.isNegative(-1)printsnegative, the closest negative value to zero.