What the code is supposed to do
The function printPositiveAndNegative receives a slice of integers, nums, and prints one line per number. A number below zero prints the word negative. Any other number, zero included, prints as itself. For []int{2, -12, 0, 4, -5, 3} the expected output is:
2
negative
0
4
negative
3
The code you start with prints nine lines instead of six:
func printPositiveAndNegative(nums []int) {
for _, num := range nums {
if num > 0 {
fmt.Println("positive")
}
fmt.Println(num)
}
}
What goes wrong
There are two separate mistakes, and both are visible in the loop body.
- The condition picks the wrong numbers.
num > 0is true for 2, 4 and 3, so those numbers get an extrapositiveline. The special word belongs to the numbers below zero, so the test must benum < 0and the word must benegative. - The last
fmt.Println(num)is unconditional. It sits after theifblock, so it runs for every number. Even when the word is printed, the number prints too, which is why-12and-5still appear. Each number must produce exactly one line, so the two prints need to be alternatives to each other.
Tracing the first three numbers of the buggy version shows it. For 2, the if prints positive and then the number: two lines. For -12, the if is skipped and only -12 prints. For 0, the same: only 0 prints.
Fixing it step by step
- Change the comparison from
num > 0tonum < 0. Zero is not less than zero, so it falls to the other case, as the task requires. - Change the message from
"positive"to"negative". - Add an
elsebranch and movefmt.Println(num)into it. Now exactly one of the two prints runs for each number. - Leave
mainFunctionalone. It already calls the function with the slice from the task.
The fixed code in Go
func printPositiveAndNegative(nums []int) {
for _, num := range nums {
if num < 0 {
fmt.Println("negative")
} else {
fmt.Println(num)
}
}
}
func mainFunction() {
printPositiveAndNegative([]int{2, -12, 0, 4, -5, 3})
}
A few Go details are worth knowing here:
rangegives two values. For a slice it yields the index and the element. The loop writes_, numbecause Go refuses to compile a program with a declared but unused variable, so the unneeded index goes to the blank identifier_. Writingfor i, num := range numswithout usingiis a compile error:i declared and not used.elsemust follow the closing brace. Go requires} else {on one line. Puttingelseon a new line is a syntax error.- No parentheses around the condition. Go writes
if num < 0 {, and the braces are mandatory even for one statement. fmt.Printlnadds a newline and prints anintin plain decimal form, so-12would print as-12. The word"negative"prints without its quotes.
Why the original looked plausible
The starting code has the right shape: a loop, a test, and a print. Bugs like this come from a branch that adds an extra action instead of choosing between two. An if without an else only adds work for some elements, while the code after it still runs for all of them. When the task says "either this or that", look for an else.
Common mistakes
- Using
<=. That would turn 0 intonegative. The task wants0printed as a number. - Fixing only the condition. With
if num < 0 { fmt.Println("negative") }followed by the unconditional print, every negative number prints twice: first the word, then the number. - Using two separate
ifstatements such asif num < 0andif num >= 0. That works, butelsesays the same thing without a second test that could drift out of sync with the first. - Misspelling the word. The output is compared as text, so
Negativeornegetivefails.
Checking your fix
Run the code in the editor above. A correct fix prints six lines: 2, negative, 0, 4, negative, 3. For an empty slice the loop body never runs and nothing prints.