Go Operator Precedence 🎯

beginner
11 min

Go Operator Precedence 🎯

Welcome to our comprehensive guide on Go Operator Precedence! This lesson is designed to help you understand the order of operations in Go, making your code cleaner, more readable, and easier to debug.

Understanding Operators 📝

Before we dive into operator precedence, let's briefly discuss what operators are and their importance in Go.

Operators are symbols that tell the Go compiler to perform specific actions on variables or values. They help us compare, assign, and manipulate data.

Operator Precedence in Go 💡

Operator precedence in Go determines the order in which operators are evaluated in expressions with multiple operators. This can greatly affect the final result of your calculations.

Basic Types of Operators 📝

  1. Arithmetic Operators (+, -, *, /, %) - used for mathematical operations.
  2. Comparison Operators (==, !=, <, <=, >, >=) - used to compare two values.
  3. Logical Operators (&&, ||, !) - used to combine conditional expressions.
  4. Assignment Operators (=, +=, -=, *=, /=, %=) - used to assign a value to a variable or modify the current value of a variable.

Precedence Rules 📝

  1. Parentheses (`): Parentheses have the highest precedence and can be used to override the default operator precedence.

  2. Unary Operators (+, -, !, *, &, ^, |, sizeof, len, &*, +x, -x, !x): These operators have the next highest precedence. They operate on a single operand.

  3. Multiplicative Operators (*, /, %): These operators perform multiplication, division, and modulus operations. They have the same level of precedence.

  4. Additive Operators (+, -): These operators perform addition and subtraction operations. They have the same level of precedence.

  5. Shifting Operators (<<, >>, >>>>): These operators perform bit shifting. They have the same level of precedence as the additive operators.

  6. Relational Operators (==, !=, <, <=, >, >=): These operators compare two operands. They have the same level of precedence.

  7. Equality Operators (==, !=): These operators check for equality or inequality. They have the same level of precedence as the relational operators.

  8. Logical AND (&&): The logical AND operator evaluates both operands and returns true only if both operands are true. It has a lower precedence than the relational and equality operators.

  9. Logical OR (||): The logical OR operator evaluates only one operand if the result can be determined. It returns true if at least one operand is true. It has a lower precedence than the logical AND operator.

  10. Logical Negation (!): The logical negation operator reverses the truth value of its operand. It has the lowest precedence of all operators.

Example 1 💡

Let's see an example to understand how operator precedence works:

go
package main import "fmt" func main() { x := 5 y := 3 z := (x + y) * 2 // Multiplicative operators are evaluated before additive operators due to precedence fmt.Println(z) // Output: 16 }

In the above example, the addition operation (x + y) is performed first due to the higher precedence of multiplicative operators, resulting in z having the value 10. Then, the multiplication operation * 2 is performed, resulting in the final value of z being 16.

Example 2 💡

Let's consider another example:

go
package main import "fmt" func main() { x := 5 y := 3 z := (x > y) && (x < 10) // Logical AND has a lower precedence than the relational operator fmt.Println(z) // Output: true }

In this example, the relational operator > is evaluated first due to its higher precedence, resulting in true for (x > y). Then, the logical AND operator && is evaluated, and since both conditions (x > y) and (x < 10) are true, the final value of z is true.

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the output of the following code snippet?