Start learning free

LESSON 75 OF 338 · CONDITIONAL STATEMENTS

And Operator in Go: Combining Conditions with &&

Lesson Read the full explanation

The Go && operator means and.

A condition using && is true only when both sides are true:

weather := "rainy"
temperature := 90

if weather == "sunny" && temperature > 85 {
    fmt.Println("Going to the beach")
} else {
    fmt.Println("Learning to code")
}

Here the temperature is high enough, but the weather is not sunny, so the whole condition is false.

Assignment
Use && to check both beach conditions.


STEP 1 OF 2
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    And Operator in Go, explained

    What the && operator does

    Often one comparison isn't enough. You may want to act only when the weather is sunny and the temperature is high. Go's && operator joins two boolean expressions into one that is true only when both sides are true. If either side is false, the whole expression is false.

    leftrightleft && right
    truetruetrue
    truefalsefalse
    falsetruefalse
    falsefalsefalse

    Using it in an if

    weather := "rainy"
    temperature := 90
    
    if weather == "sunny" && temperature > 85 {
        fmt.Println("Going to the beach")
    } else {
        fmt.Println("Learning to code")
    }
    

    The temperature test, temperature > 85, is true. The weather test, weather == "sunny", is false, because the value is "rainy". One false side makes the whole condition false, so the else block runs and the program prints Learning to code. Change weather to "sunny" and both sides become true, so it prints Going to the beach.

    Each side is a complete condition

    Both operands of && must be boolean expressions on their own, so each half repeats its variable. To test an age range you write age >= 13 && age <= 19, not "age is at least 13 and at most 19" squeezed into one comparison. Comparisons bind tighter than &&, so no parentheses are needed around them.

    Go-specific details

    • Spelling. Go writes "and" as && only. The word and is a syntax error (unexpected and, expected {).
    • One & is a different operator. A single & is bitwise AND on integers (and the address-of operator). It doesn't join conditions, and using it between a string comparison and a number comparison doesn't compile.
    • Short-circuit evaluation. Go evaluates the left side first. If it is false, the right side is not evaluated at all, because the answer can't change. This is more than an optimisation, as the next example shows.

    Guarding a risky check

    Short-circuiting lets the left side protect the right side:

    nums := []int{}
    
    if len(nums) > 0 && nums[0] == 5 {
        fmt.Println("first is 5")
    } else {
        fmt.Println("no")
    }
    

    This prints no. The slice is empty, so len(nums) > 0 is false and nums[0] is never evaluated. Reversing the two sides would read an index that doesn't exist and make the program panic.

    Related operators

    Go has two more logical operators that combine with &&. || means "or": true when at least one side is true. ! flips a boolean. With the lesson's starting values ("rainy" and 90), weather == "sunny" || temperature > 85 is true, and !(weather == "sunny") && temperature > 85 is also true.

    When you mix them, && is evaluated before ||, like multiplication before addition. So true || false && false is true, while (true || false) && false is false. Add parentheses whenever the grouping isn't obvious at a glance.

    The exercise

    The starter code declares weather := "rainy" and temperature := 90. The task is to print "Going to the beach" only when the weather is "sunny" and the temperature is above 85, and otherwise print "Learning to code".

    1. Write the if with both checks joined by &&. String equality uses == and the temperature test uses >. Print the beach message inside the block.
    2. Add the else block on the closing-brace line and print the other message, so the program responds when either check fails.
    weather := "rainy"
    temperature := 90
    
    if weather == "sunny" && temperature > 85 {
        fmt.Println("Going to the beach")
    } else {
        fmt.Println("Learning to code")
    }
    

    With the starting values the output is Learning to code.

    To check every outcome, try the four combinations in the editor above. "sunny" with 90 and "sunny" with 70 differ only in the temperature, and "rainy" with 90 and "rainy" with 70 differ only in the weather. Only sunny with 90 prints Going to the beach; each of the other three prints Learning to code, because one side is false in each of them. A temperature of exactly 85 also prints Learning to code, since 85 is not above 85.

    Common mistakes

    • Using and or a single &.
    • Writing = instead of == in the weather check.
    • Using >= when the rule says "above". A temperature of exactly 85 isn't above 85.
    • Wrong capitalisation. "Sunny" is not equal to "sunny".
    • Putting else on a new line, which Go rejects.

    Key takeaways

    • a && b is true only when both are true.
    • Each operand must be a full boolean expression.
    • The right side is skipped when the left side is already false.
    • Go has no and keyword.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free