Go > Core Go Basics > Basic Operators > Logical Operators (&&, ||, !)

Go Logical Operators: AND, OR, NOT

This snippet demonstrates the use of logical operators (&&, ||, !) in Go. These operators are crucial for controlling program flow based on multiple conditions.

Introduction to Logical Operators

Logical operators are fundamental building blocks in programming, allowing you to combine or negate boolean expressions. Go provides three essential logical operators: && (AND), || (OR), and ! (NOT).

AND (&&) Operator

The && operator returns true if and only if both operands are true. Otherwise, it returns false. In the example, only when both a and b are true, the result is true. If the first operand is false, the second operand is not evaluated (short-circuiting).

package main

import "fmt"

func main() {
	var a = true
	var b = false

	result := a && b
	fmt.Printf("a && b is: %t\n", result)

	a = true
	b = true
	result = a && b
	fmt.Printf("a && b is: %t\n", result)
}

OR (||) Operator

The || operator returns true if at least one of the operands is true. It returns false only when both operands are false. If the first operand is true, the second operand is not evaluated (short-circuiting).

package main

import "fmt"

func main() {
	var a = true
	var b = false

	result := a || b
	fmt.Printf("a || b is: %t\n", result)

	a = false
	b = false
	result = a || b
	fmt.Printf("a || b is: %t\n", result)
}

NOT (!) Operator

The ! operator negates the value of its operand. If the operand is true, it returns false, and vice versa.

package main

import "fmt"

func main() {
	var a = true

	result := !a
	fmt.Printf("!a is: %t\n", result)

	a = false
	result = !a
	fmt.Printf("!a is: %t\n", result)
}

Combining Logical Operators

Logical operators can be combined to create more complex conditions. In this example, we check if a person can rent a car based on their age and whether they have a license. We also check if a person will go out based on whether it's the weekend or they have money.

package main

import "fmt"

func main() {
	age := 25
	hasLicense := true

	canRentCar := age >= 21 && hasLicense
	fmt.Printf("Can rent a car: %t\n", canRentCar)

	isWeekend := true
	hasMoney := false

	goOut := isWeekend || hasMoney
	fmt.Printf("Going out: %t\n", goOut)
}

Real-Life Use Case: Input Validation

Logical operators are frequently used for input validation. For example, you might want to ensure that a user-provided age is within a valid range and that a required field is not empty.

Best Practices

  • Use parentheses to clarify the order of operations when combining multiple logical operators.
  • Keep logical expressions concise and easy to understand.
  • Avoid overly complex nested conditions, as they can be difficult to debug.

Interview Tip

Be prepared to explain the short-circuiting behavior of && and ||. Understand the truth tables for each operator.

When to use them

Use logical operators when you need to make decisions based on multiple conditions. This is common in control flow statements like if, else if, and for.

Alternatives

While logical operators are the standard way to combine boolean expressions, sometimes you can simplify complex logic using techniques like truth tables and boolean algebra to reduce the number of operators needed. However, readability is often more important than minimizing the number of operations.

Pros

  • Logical operators are fundamental and universally understood.
  • They are efficient due to short-circuiting behavior.
  • They improve code readability when used correctly.

Cons

  • Overly complex logical expressions can reduce code readability.
  • Misunderstanding short-circuiting behavior can lead to unexpected results.

FAQ

  • What is short-circuiting in logical operators?

    Short-circuiting means that the second operand of && and || is not evaluated if the result of the expression can be determined from the first operand alone. For &&, if the first operand is false, the result is always false. For ||, if the first operand is true, the result is always true.
  • What is the precedence of logical operators in Go?

    The precedence of logical operators in Go is as follows (highest to lowest): !, &&, ||. Use parentheses to override the default precedence.