Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Operators in Golang

Operators in Go (often referred to as Golang) are special symbols that perform operations on variables and values. These operators allow you to manipulate data and make decisions based on it. Here's a detailed tutorial on operators in Go:

1. Arithmetic Operators

These are used for mathematical operations:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (Remainder)
  • ++: Increment
  • --: Decrement

2. Relational Operators

Used for comparing two values:

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

3. Logical Operators

Used to combine conditional statements:

  • &&: Logical AND (returns true if both operands are true)
  • ||: Logical OR (returns true if at least one operand is true)
  • !: Logical NOT (reverses the logic)

4. Assignment Operators

Used to assign values:

  • =: Assign
  • +=: Add and assign
  • -=: Subtract and assign
  • *=: Multiply and assign
  • /=: Divide and assign
  • %=: Modulus and assign

5. Bitwise Operators

Operate at the bit level:

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • <<: Left shift
  • >>: Right shift
  • ^: Bitwise NOT (it's the same as XOR but acts as NOT when applied to a single operand)

6. Miscellaneous Operators

  • &: Address of (returns the memory address of a variable)
  • *: Pointer dereference (returns the value of the variable a pointer is pointing to)

Examples:

Let's see a few examples for a better understanding:

package main

import "fmt"

func main() {
    a := 10
    b := 20

    fmt.Println("a + b =", a + b)  // 30
    fmt.Println("a < b", a < b)    // true
    fmt.Println("a & b", a & b)    // 0 (in binary: 1010 & 10100 = 00000)
    fmt.Println("a | b", a | b)    // 30 (in binary: 1010 | 10100 = 11110)
    
    p := &a
    fmt.Println("Address of a:", p)  // Memory address of variable a
    fmt.Println("Value using pointer:", *p) // 10
}

Conclusion:

Operators are fundamental building blocks in Golang, allowing for a wide range of operations, from basic arithmetic to complex logical and bitwise manipulations. Familiarity with these operators is key to writing efficient and effective Go programs.

  1. Arithmetic Operators in Golang:

    • Arithmetic operators perform basic mathematical operations.
      package main
      
      import "fmt"
      
      func main() {
          a, b := 5, 2
      
          fmt.Println("Addition:", a+b)    // Output: 7
          fmt.Println("Subtraction:", a-b) // Output: 3
          fmt.Println("Multiplication:", a*b) // Output: 10
          fmt.Println("Division:", a/b)       // Output: 2
          fmt.Println("Modulus:", a%b)        // Output: 1
      }
      
  2. Relational Operators in Golang:

    • Relational operators compare values and return a boolean result.
      package main
      
      import "fmt"
      
      func main() {
          a, b := 5, 2
      
          fmt.Println("Equal:", a == b)  // Output: false
          fmt.Println("Not Equal:", a != b) // Output: true
          fmt.Println("Greater Than:", a > b)   // Output: true
          fmt.Println("Less Than:", a < b)      // Output: false
          fmt.Println("Greater or Equal:", a >= b) // Output: true
          fmt.Println("Less or Equal:", a <= b)    // Output: false
      }
      
  3. Logical Operators in Golang:

    • Logical operators perform boolean logic operations.
      package main
      
      import "fmt"
      
      func main() {
          x, y := true, false
      
          fmt.Println("AND:", x && y) // Output: false
          fmt.Println("OR:", x || y)  // Output: true
          fmt.Println("NOT:", !x)     // Output: false
      }
      
  4. Bitwise Operators in Golang:

    • Bitwise operators perform operations at the bit level.
      package main
      
      import "fmt"
      
      func main() {
          a, b := 5, 3
      
          fmt.Println("AND:", a&b)  // Output: 1
          fmt.Println("OR:", a|b)   // Output: 7
          fmt.Println("XOR:", a^b)  // Output: 6
          fmt.Println("Shift Left:", a<<1) // Output: 10
          fmt.Println("Shift Right:", a>>1) // Output: 2
      }
      
  5. Assignment Operators in Golang:

    • Assignment operators perform both the operation and assignment in a concise way.
      package main
      
      import "fmt"
      
      func main() {
          a := 5
      
          a += 2 // Equivalent to a = a + 2
          fmt.Println("Value of a:", a) // Output: 7
      
          a -= 3 // Equivalent to a = a - 3
          fmt.Println("Value of a:", a) // Output: 4
      
          a *= 2 // Equivalent to a = a * 2
          fmt.Println("Value of a:", a) // Output: 8
      
          a /= 4 // Equivalent to a = a / 4
          fmt.Println("Value of a:", a) // Output: 2
      }
      
  6. Comparison Operators in Golang:

    • Comparison operators compare values and return a boolean result.
      package main
      
      import "fmt"
      
      func main() {
          a, b := 5, 2
      
          fmt.Println("Equal:", a == b)  // Output: false
          fmt.Println("Not Equal:", a != b) // Output: true
      }
      
  7. Unary Operators in Golang:

    • Unary operators operate on a single operand.
      package main
      
      import "fmt"
      
      func main() {
          a := 5
      
          fmt.Println("Unary Plus:", +a)  // Output: 5
          fmt.Println("Unary Minus:", -a) // Output: -5
          fmt.Println("Logical NOT:", !true) // Output: false
      }
      
  8. Overloaded Operators in Golang:

    • Golang does not support operator overloading, and each operator has a fixed set of operations.
      // Operator overloading is not supported in Golang.