In Go, operators are special symbols or keywords used to perform operations on variables and values. They are essential for manipulating data and controlling the flow of a program. This section will cover the different types of operators available in Go, including arithmetic, relational, logical, bitwise, and assignment operators.

Types of Operators

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Assignment Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (remainder) a % b

Example:

package main

import "fmt"

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

    fmt.Println("Addition:", a + b)        // 13
    fmt.Println("Subtraction:", a - b)     // 7
    fmt.Println("Multiplication:", a * b)  // 30
    fmt.Println("Division:", a / b)        // 3
    fmt.Println("Modulus:", a % b)         // 1
}

  1. Relational Operators

Relational operators are used to compare two values. They return a boolean value (true or false).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal a >= b
<= Less than or equal a <= b

Example:

package main

import "fmt"

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

    fmt.Println("Equal to:", a == b)        // false
    fmt.Println("Not equal to:", a != b)    // true
    fmt.Println("Greater than:", a > b)     // true
    fmt.Println("Less than:", a < b)        // false
    fmt.Println("Greater or equal:", a >= b) // true
    fmt.Println("Less or equal:", a <= b)   // false
}

  1. Logical Operators

Logical operators are used to combine multiple boolean expressions or values.

Operator Description Example
&& Logical AND a && b
` `
! Logical NOT !a

Example:

package main

import "fmt"

func main() {
    a := true
    b := false

    fmt.Println("Logical AND:", a && b)     // false
    fmt.Println("Logical OR:", a || b)      // true
    fmt.Println("Logical NOT:", !a)         // false
}

  1. Bitwise Operators

Bitwise operators perform bit-level operations on integer types.

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
<< Left shift a << b
>> Right shift a >> b

Example:

package main

import "fmt"

func main() {
    a := 10  // 1010 in binary
    b := 3   // 0011 in binary

    fmt.Println("Bitwise AND:", a & b)      // 2 (0010 in binary)
    fmt.Println("Bitwise OR:", a | b)       // 11 (1011 in binary)
    fmt.Println("Bitwise XOR:", a ^ b)      // 9 (1001 in binary)
    fmt.Println("Left shift:", a << 1)      // 20 (10100 in binary)
    fmt.Println("Right shift:", a >> 1)     // 5 (0101 in binary)
}

  1. Assignment Operators

Assignment operators are used to assign values to variables. They can also be combined with arithmetic and bitwise operators.

Operator Description Example
= Assign a = b
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
%= Modulus and assign a %= b
&= Bitwise AND and assign a &= b
` =` Bitwise OR and assign
^= Bitwise XOR and assign a ^= b
<<= Left shift and assign a <<= b
>>= Right shift and assign a >>= b

Example:

package main

import "fmt"

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

    a += b
    fmt.Println("Add and assign:", a)       // 13

    a -= b
    fmt.Println("Subtract and assign:", a)  // 10

    a *= b
    fmt.Println("Multiply and assign:", a)  // 30

    a /= b
    fmt.Println("Divide and assign:", a)    // 10

    a %= b
    fmt.Println("Modulus and assign:", a)   // 1
}

Practical Exercises

Exercise 1: Basic Arithmetic Operations

Write a Go program that takes two integers as input and performs all arithmetic operations on them.

Solution:

package main

import "fmt"

func main() {
    var a, b int
    fmt.Print("Enter first number: ")
    fmt.Scan(&a)
    fmt.Print("Enter second number: ")
    fmt.Scan(&b)

    fmt.Println("Addition:", a + b)
    fmt.Println("Subtraction:", a - b)
    fmt.Println("Multiplication:", a * b)
    fmt.Println("Division:", a / b)
    fmt.Println("Modulus:", a % b)
}

Exercise 2: Relational and Logical Operations

Write a Go program that takes two integers as input and prints the results of relational and logical operations.

Solution:

package main

import "fmt"

func main() {
    var a, b int
    fmt.Print("Enter first number: ")
    fmt.Scan(&a)
    fmt.Print("Enter second number: ")
    fmt.Scan(&b)

    fmt.Println("Equal to:", a == b)
    fmt.Println("Not equal to:", a != b)
    fmt.Println("Greater than:", a > b)
    fmt.Println("Less than:", a < b)
    fmt.Println("Greater or equal:", a >= b)
    fmt.Println("Less or equal:", a <= b)

    fmt.Println("Logical AND:", (a > 0) && (b > 0))
    fmt.Println("Logical OR:", (a > 0) || (b > 0))
    fmt.Println("Logical NOT:", !(a > 0))
}

Exercise 3: Bitwise Operations

Write a Go program that takes two integers as input and performs bitwise operations on them.

Solution:

package main

import "fmt"

func main() {
    var a, b int
    fmt.Print("Enter first number: ")
    fmt.Scan(&a)
    fmt.Print("Enter second number: ")
    fmt.Scan(&b)

    fmt.Println("Bitwise AND:", a & b)
    fmt.Println("Bitwise OR:", a | b)
    fmt.Println("Bitwise XOR:", a ^ b)
    fmt.Println("Left shift:", a << 1)
    fmt.Println("Right shift:", a >> 1)
}

Conclusion

In this section, we covered the various types of operators in Go, including arithmetic, relational, logical, bitwise, and assignment operators. We also provided practical examples and exercises to help reinforce the concepts. Understanding these operators is crucial for performing operations on data and controlling the flow of your Go programs. In the next section, we will delve into control structures, which will further enhance your ability to write complex and efficient Go programs.

© Copyright 2024. All rights reserved