Go Operators

In Go, operators are symbols that perform various operations on operands (variables, constants, or expressions). Here are the different types of operators available in Go:

  1. Arithmetic Operators: Used to perform mathematical operations on numerical values.
  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Remainder (%)
  • Increment (++)
  • Decrement (–)
  1. Comparison Operators: Used to compare values and produce a boolean result.
  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
  1. Logical Operators: Used to perform logical operations on boolean values.
  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)
  1. Assignment Operators: Used to assign values to variables.
  • Simple assignment (=)
  • Add and assign (+=)
  • Subtract and assign (-=)
  • Multiply and assign (*=)
  • Divide and assign (/=)
  • Remainder and assign (%=)
  1. Bitwise Operators: Used to perform operations on individual bits of integer values.
  • Bitwise AND (&)
  • Bitwise OR (|)
  • Bitwise XOR (^)
  • Bitwise NOT (^)
  • Left shift (<<)
  • Right shift (>>)
  1. Unary Operators: Used to perform operations on a single operand.
  • Unary plus (+)
  • Unary minus (-)
  • Logical NOT (!)
  • Bitwise NOT (^)
  1. Other Operators:
  • Address operator (&): Returns the address of a variable.
  • Pointer dereference operator (*): Accesses the value stored at a memory address.

These operators allow you to perform a wide range of operations in Go, such as arithmetic calculations, logical evaluations, bit manipulation, and variable assignments. Understanding and utilizing operators correctly is essential for writing efficient and effective Go programs.

Go Arithmetic Operators.

In Go, arithmetic operators are used to perform mathematical calculations on numerical values. Here are the arithmetic operators available in Go:

  1. Addition (+): Adds two operands together.
   x := 5
   y := 3
   result := x + y
   fmt.Println(result) // Output: 8
  1. Subtraction (-): Subtracts the second operand from the first operand.
   x := 10
   y := 4
   result := x - y
   fmt.Println(result) // Output: 6
  1. Multiplication (*): Multiplies two operands together.
   x := 6
   y := 7
   result := x * y
   fmt.Println(result) // Output: 42
  1. Division (/): Divides the first operand by the second operand.
   x := 15
   y := 3
   result := x / y
   fmt.Println(result) // Output: 5
  1. Remainder/Modulus (%): Computes the remainder when the first operand is divided by the second operand.
   x := 20
   y := 7
   result := x % y
   fmt.Println(result) // Output: 6
  1. Increment (++): Increases the value of a variable by 1.
   x := 5
   x++
   fmt.Println(x) // Output: 6
  1. Decrement (–): Decreases the value of a variable by 1.
   x := 8
   x--
   fmt.Println(x) // Output: 7

These arithmetic operators allow you to perform basic mathematical operations on numerical values in Go.

Go Assignment Operators

In Go, assignment operators are used to assign values to variables. They combine the assignment operation with another operation, making it more concise. Here are the assignment operators available in Go:

  1. Simple assignment (=): Assigns the value on the right-hand side to the variable on the left-hand side.
   x := 10
  1. Add and assign (+=): Adds the value on the right-hand side to the variable on the left-hand side and assigns the result back to the variable.
   x := 5
   x += 3  // equivalent to x = x + 3
   fmt.Println(x) // Output: 8
  1. Subtract and assign (-=): Subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result back to the variable.
   x := 10
   x -= 4  // equivalent to x = x - 4
   fmt.Println(x) // Output: 6
  1. Multiply and assign (*=): Multiplies the variable on the left-hand side by the value on the right-hand side and assigns the result back to the variable.
   x := 3
   x *= 5  // equivalent to x = x * 5
   fmt.Println(x) // Output: 15
  1. Divide and assign (/=): Divides the variable on the left-hand side by the value on the right-hand side and assigns the result back to the variable.
   x := 20
   x /= 4  // equivalent to x = x / 4
   fmt.Println(x) // Output: 5
  1. Remainder and assign (%=): Computes the remainder when the variable on the left-hand side is divided by the value on the right-hand side and assigns the result back to the variable.
   x := 15
   x %= 7  // equivalent to x = x % 7
   fmt.Println(x) // Output: 1

Assignment operators provide a concise way to update the value of a variable based on its current value and another value. They can help make your code more readable and efficient by combining assignment and arithmetic operations in a single statement.

Go Comparison Operators.

In Go, comparison operators are used to compare values and produce a boolean result (true or false) based on the comparison. Here are the comparison operators available in Go:

  1. Equal to (==): Checks if two values are equal.
   x := 5
   y := 7
   result := x == y
   fmt.Println(result) // Output: false
  1. Not equal to (!=): Checks if two values are not equal.
   x := 5
   y := 7
   result := x != y
   fmt.Println(result) // Output: true
  1. Greater than (>): Checks if the left operand is greater than the right operand.
   x := 10
   y := 7
   result := x > y
   fmt.Println(result) // Output: true
  1. Less than (<): Checks if the left operand is less than the right operand.
   x := 5
   y := 7
   result := x < y
   fmt.Println(result) // Output: true
  1. Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
   x := 10
   y := 10
   result := x >= y
   fmt.Println(result) // Output: true
  1. Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
   x := 5
   y := 7
   result := x <= y
   fmt.Println(result) // Output: true

Comparison operators are useful for making decisions and performing conditional operations in your Go programs. They allow you to compare values and determine relationships between them. The result of a comparison operation is a boolean value, which can be used in conditional statements, loops, and other logical operations.

Go Logical Operators and Examples.

In Go, logical operators are used to perform logical operations on boolean values and produce a boolean result. Here are the logical operators available in Go:

  1. Logical AND (&&): Returns true if both operands are true.
   x := true
   y := false
   result := x && y
   fmt.Println(result) // Output: false
  1. Logical OR (||): Returns true if at least one of the operands is true.
   x := true
   y := false
   result := x || y
   fmt.Println(result) // Output: true
  1. Logical NOT (!): Negates the boolean value of the operand.
   x := true
   result := !x
   fmt.Println(result) // Output: false

Logical operators are typically used in conditional statements and control flow constructs to evaluate conditions and determine the flow of execution. They allow you to combine multiple conditions and perform complex logical evaluations.

Go Bitwise Operators.

In Go, bitwise operators are used to perform bitwise operations on integer values at the bit level. Here are the bitwise operators available in Go:

  1. Bitwise AND (&): Performs a bitwise AND operation between the corresponding bits of two operands.
   x := 5 // 101 in binary
   y := 3 // 011 in binary
   result := x & y
   fmt.Println(result) // Output: 1 (001 in binary)
  1. Bitwise OR (|): Performs a bitwise OR operation between the corresponding bits of two operands.
   x := 5 // 101 in binary
   y := 3 // 011 in binary
   result := x | y
   fmt.Println(result) // Output: 7 (111 in binary)
  1. Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation between the corresponding bits of two operands.
   x := 5 // 101 in binary
   y := 3 // 011 in binary
   result := x ^ y
   fmt.Println(result) // Output: 6 (110 in binary)
  1. Bitwise NOT (~): Flips the bits of the operand, producing the complement.
   x := 5 // 101 in binary
   result := ^x
   fmt.Println(result) // Output: -6 (complement of 101 is 010, which represents -6 in 2's complement form)
  1. Bitwise left shift (<<): Shifts the bits of the left operand to the left by a specified number of positions.
   x := 5 // 101 in binary
   result := x << 2
   fmt.Println(result) // Output: 20 (10100 in binary)
  1. Bitwise right shift (>>): Shifts the bits of the left operand to the right by a specified number of positions.
   x := 10 // 1010 in binary
   result := x >> 2
   fmt.Println(result) // Output: 2 (10 in binary)

Bitwise operators are commonly used in low-level programming, networking, and working with binary data. They allow you to manipulate individual bits of integer values to perform various operations and optimizations.