Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Go Programming - Operators

Introduction

In Go programming, operators are special symbols that perform operations on variables and values. Go supports several types of operators, including arithmetic, relational, logical, bitwise, assignment, and miscellaneous operators.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulus
Example:
x := 10
y := 5
fmt.Println(x + y)  // Output: 15
fmt.Println(x - y)  // Output: 5
fmt.Println(x * y)  // Output: 50
fmt.Println(x / y)  // Output: 2
fmt.Println(x % y)  // Output: 0
                

Relational Operators

Relational operators are used to compare two values. They return a boolean result.

  • == : Equal to
  • != : Not equal to
  • > : Greater than
  • < : Less than
  • >= : Greater than or equal to
  • <= : Less than or equal to
Example:
a := 10
b := 20
fmt.Println(a == b)  // Output: false
fmt.Println(a != b)  // Output: true
fmt.Println(a > b)   // Output: false
fmt.Println(a < b)   // Output: true
fmt.Println(a >= b)  // Output: false
fmt.Println(a <= b)  // Output: true
                

Logical Operators

Logical operators are used to combine conditional statements.

  • && : Logical AND
  • || : Logical OR
  • ! : Logical NOT
Example:
a := true
b := false
fmt.Println(a && b)  // Output: false
fmt.Println(a || b)  // Output: true
fmt.Println(!a)      // Output: false
                

Bitwise Operators

Bitwise operators perform bit-level operations on operands.

  • & : Bitwise AND
  • | : Bitwise OR
  • ^ : Bitwise XOR
  • << : Left shift
  • >> : Right shift
Example:
a := 5    // 0101 in binary
b := 3    // 0011 in binary
fmt.Println(a & b)   // Output: 1 (0001 in binary)
fmt.Println(a | b)   // Output: 7 (0111 in binary)
fmt.Println(a ^ b)   // Output: 6 (0110 in binary)
fmt.Println(a << 1)  // Output: 10 (1010 in binary)
fmt.Println(a >> 1)  // Output: 2 (0010 in binary)
                

Assignment Operators

Assignment operators are used to assign values to variables.

  • = : Simple assignment
  • += : Add and assign
  • -= : Subtract and assign
  • *= : Multiply and assign
  • /= : Divide and assign
  • %= : Modulus and assign
Example:
var x int
x = 10     // Simple assignment
x += 5     // Equivalent to x = x + 5
x -= 3     // Equivalent to x = x - 3
x *= 2     // Equivalent to x = x * 2
x /= 2     // Equivalent to x = x / 2
x %= 3     // Equivalent to x = x % 3
fmt.Println(x)  // Output: 1
                

Miscellaneous Operators

Go also supports some miscellaneous operators.

  • & : Address of
  • * : Pointer
Example:
var a int = 42
var b *int = &a    // 'b' is a pointer to 'a'
fmt.Println(a)     // Output: 42
fmt.Println(&a)    // Output: Address of 'a'
fmt.Println(b)     // Output: Address of 'a'
fmt.Println(*b)    // Output: 42 (Value at the address stored in 'b')