Start learning free

LESSON 70 OF 338 · CONDITIONAL STATEMENTS

Is Number Negative in Go: if/else and fmt.Println

Practice Read the full explanation

Create a function that takes a number n as its only argument and prints "negative" to the console if n is less than zero, otherwise prints "positive" to the console.

Example 1:

Input: n = 5
Output: "positive"
Explanation:
5 is greater than or equal to zero, so our function prints "positive" to the console.

Example 2:

Input: n = 0
Output: "positive"
Explanation:
0 is greater than or equal to zero, so our function prints "positive" to the console.

Example 3:

Input: n = -2
Output: "negative"
Explanation:
-2 is less than zero, so our function prints "negative" to the console.

Hints:

You can make use of < operator with an if statement to check if n is negative.

You can add an else statement to handle the case when n is positive.
STEP 1 OF 3
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Is Number Negative in Go, explained

    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

    1. Write the if with its condition. The test is n < 0. Go takes no parentheses around it, and the body goes in braces.
    2. Print negative inside the block with fmt.Println("negative"). Println ends the output with a line break, which is what the lesson's tests expect.
    3. Add the else branch and print positive in it. It needs no condition: it runs for every n that 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.Println writes 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 < 0 is false for zero. Writing n <= 0 would 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 int means the function accepts whole numbers only. Passing a value such as 2.5 is a compile error, because Go won't convert it to an int for you. On this platform an int is 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) prints positive.
    • Using <=. It makes zero print negative, which breaks Example 2.
    • Using return instead of printing. The function has no result type, so return "negative" doesn't compile, and the tests look at what is printed.
    • Misspelled output. The text must be exactly negative or positive, in lowercase.
    • The largest and smallest integers. A single comparison never overflows, so the function works for every int value, unlike code that negates the number first.

    Testing

    • isNegative(5) prints positive.
    • isNegative(0) prints positive.
    • isNegative(-2) prints negative.
    • isNegative(-1) prints negative, the closest negative value to zero.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free