Go Operators

Introduction

Operators are special symbols that perform operations on variables and values. In Go, operators are used in arithmetic, logical, relational, bitwise, and other types of operations. In this chapter, you will learn different types of operators available in Go, with examples for each type.

Types of Operators in Go

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

  • Addition (+): Adds two operands.
result := 5 + 3  // result is 8
  • Subtraction (-): Subtracts the right operand from the left operand.
result := 5 - 3  // result is 2
  • Multiplication (*): Multiplies two operands.
result := 5 * 3  // result is 15
  • Division (/): Divides the left operand by the right operand.
result := 6 / 3  // result is 2
  • Modulus (%): Returns the remainder of the division.
result := 5 % 3  // result is 2

Relational Operators

Relational operators compare two operands and return a boolean result.

  • Equal to (==): Checks if two operands are equal.
result := (5 == 3)  // result is false
  • Not equal to (!=): Checks if two operands are not equal.
result := (5 != 3)  // result is true
  • Greater than (>): Checks if the left operand is greater than the right operand.
result := (5 > 3)  // result is true
  • Less than (<): Checks if the left operand is less than the right operand.
result := (5 < 3)  // result is false
  • Greater than or equal to (>=): Checks if the left operand is greater than or equal to the right operand.
result := (5 >= 3)  // result is true
  • Less than or equal to (<=): Checks if the left operand is less than or equal to the right operand.
result := (5 <= 3)  // result is false

Logical Operators

Logical operators are used to combine or invert boolean values.

  • Logical AND (&&): Returns true if both operands are true.
result := (true && false)  // result is false
  • Logical OR (||): Returns true if at least one operand is true.
result := (true || false)  // result is true
  • Logical NOT (!): Inverts the boolean value.
result := !true  // result is false

Bitwise Operators

Bitwise operators perform bit-level operations on integer types.

  • AND (&): Performs a bitwise AND operation.
result := 5 & 3  // result is 1 (0101 & 0011 = 0001)
  • OR (|): Performs a bitwise OR operation.
result := 5 | 3  // result is 7 (0101 | 0011 = 0111)
  • XOR (^): Performs a bitwise XOR operation.
result := 5 ^ 3  // result is 6 (0101 ^ 0011 = 0110)
  • AND NOT (&^): Performs a bitwise AND NOT operation.
result := 5 &^ 3  // result is 4 (0101 &^ 0011 = 0100)
  • Left Shift (<<): Shifts bits to the left.
result := 5 << 1  // result is 10 (0101 << 1 = 1010)
  • Right Shift (>>): Shifts bits to the right.
result := 5 >> 1  // result is 2 (0101 >> 1 = 0010)

Assignment Operators

Assignment operators are used to assign values to variables.

  • Assign (=): Assigns the right operand to the left operand.
var x int
x = 5  // x is 5
  • Add and assign (+=): Adds the right operand to the left operand and assigns the result to the left operand.
var x int = 5
x += 3  // x is 8
  • Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
var x int = 5
x -= 3  // x is 2
  • Multiply and assign (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.
var x int = 5
x *= 3  // x is 15
  • Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.
var x int = 6
x /= 3  // x is 2
  • Modulus and assign (%=): Takes the modulus using two operands and assigns the result to the left operand.
var x int = 5
x %= 3  // x is 2

Increment and Decrement Operators

Increment and decrement operators increase or decrease the value of an integer by one.

  • Increment (++): Increases the value by one.
var x int = 5
x++  // x is 6
  • Decrement (–): Decreases the value by one.
var x int = 5
x--  // x is 4

Conclusion

Go offers a variety of operators for performing arithmetic, logical, relational, bitwise, and assignment operations. Understanding how to use these operators is crucial for writing efficient and effective Go programs. By mastering these operators, you can perform complex computations and manipulate data with ease.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top