Operation in Go

In Go, operations play a crucial role in manipulating data and controlling program flow. Understanding operations, from basic arithmetic to advanced concurrency operations, is essential for writing efficient and effective Go programs. This chapter aims to provide a comprehensive guide to operations in Go, covering everything from fundamental arithmetic operations to advanced concepts like goroutines and channels.

Basic Arithmetic Operations

Go supports all the basic arithmetic operations: addition, subtraction, multiplication, division, and modulus. Let’s explore each operation with examples:

Addition:

				
					package main

import "fmt"

func main() {
    result := 10 + 5 
    fmt.Println("Result of addition:", result) // Output: Result of addition: 15
}

				
			

Explanation:

  • We declare a variable result and assign it the sum of 10 and 5.
  • The fmt.Println() function is used to print the result to the console.

Subtraction:

				
					package main

import "fmt"

func main() {
    result := 10 - 5
    fmt.Println("Result of subtraction:", result) // Output: Result of subtraction: 5
}

				
			

Explanation:

  • Similar to addition, we declare a variable result and assign it the difference of 10 and 5.

Multiplication:

				
					package main

import "fmt"

func main() {
    result := 10 * 5
    fmt.Println("Result of multiplication:", result) // Output: Result of multiplication: 50
}

				
			

Explanation:

  • We declare a variable result and assign it the product of 10 and 5.

Division:

				
					package main

import "fmt"

func main() {
    result := 10 / 5
    fmt.Println("Result of division:", result) // Output: Result of division: 2
}

				
			

Explanation:

  • We declare a variable result and assign it the quotient of 10 divided by 5.

Modulus:

				
					package main

import "fmt"

func main() {
    result := 10 % 3
    fmt.Println("Result of modulus:", result) // Output: Result of modulus: 1
}

				
			

Explanation

We declare a variable result and assign it the remainder of 10 divided by 3

Comparison and Logical Operations

In addition to arithmetic operations, Go also provides comparison and logical operations to control program flow and make decisions. Let’s explore these operations with examples:

Comparison Operations:

Go supports various comparison operators such as == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). Let’s see some examples:

				
					package main

import "fmt"

func main() {
    // Equal to
    fmt.Println(10 == 5)  // false

    // Not equal to
    fmt.Println(10 != 5)  // true

    // Greater than
    fmt.Println(10 > 5)   // true

    // Less than
    fmt.Println(10 < 5)   // false

    // Greater than or equal to
    fmt.Println(10 >= 5)  // true

    // Less than or equal to
    fmt.Println(10 <= 5)  // false
}

				
			
  • We use various comparison operators to compare two values.
  • The result of each comparison is printed to the console using fmt.Println().

Logical Operations:

Go provides logical operators such as && (logical AND), || (logical OR), and ! (logical NOT). Let’s see how they work:

				
					package main

import "fmt"

func main() {
    x := 10
    y := 5

    // Logical AND
    fmt.Println(x > 5 && y > 2)   // true

    // Logical OR
    fmt.Println(x > 5 || y < 2)   // true

    // Logical NOT
    fmt.Println(!(x == y))        // true
}

				
			
  • We use logical operators to combine multiple conditions.
  • The result of each logical operation is printed to the console.

Bitwise Operations

In addition to arithmetic, comparison, and logical operations, Go also supports bitwise operations for manipulating individual bits within integer values. Let’s explore bitwise AND, OR, XOR, left shift, and right shift operations with examples:

Bitwise AND:

				
					package main

import "fmt"

func main() {
    result := 10 & 5
    fmt.Println("Result of bitwise AND:", result) // Output: Result of bitwise AND: 0
}

				
			

We declare a variable result and assign it the result of performing a bitwise AND operation between 10 and 5.

Bitwise OR:

				
					package main

import "fmt"

func main() {
    result := 10 | 5
    fmt.Println("Result of bitwise OR:", result) // Output: Result of bitwise OR: 15
}

				
			
  • We declare a variable result and assign it the result of performing a bitwise OR operation between 10 and 5.

Bitwise XOR:

				
					package main

import "fmt"

func main() {
    result := 10 ^ 5
    fmt.Println("Result of bitwise XOR:", result) // Output: Result of bitwise XOR: 15
}

				
			
  • We declare a variable result and assign it the result of performing a bitwise XOR operation between 10 and 5.

Left Shift:

				
					package main

import "fmt"

func main() {
    result := 10 << 2
    fmt.Println("Result of left shift:", result) // Output: Result of left shift: 40
}

				
			
  • We declare a variable result and assign it the result of left shifting the binary representation of 10 by 2 positions.

Right Shift:

				
					package main

import "fmt"

func main() {
    result := 10 >> 2
    fmt.Println("Result of right shift:", result) // Output: Result of right shift: 2
}

				
			
  • We declare a variable result and assign it the result of right shifting the binary representation of 10 by 2 positions.

Advanced Operations with Goroutines and Channels

One of the unique features of Go is its built-in support for concurrency through goroutines and channels. Goroutines are lightweight threads managed by the Go runtime, and channels are used for communication and synchronization between goroutines. Let’s see how we can leverage goroutines and channels for advanced operations:

Goroutines:

				
					package main

import (
    "fmt"
    "time"
)

func printNumbers() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
        time.Sleep(1 * time.Second)
    }
}

func main() {
    go printNumbers()
    time.Sleep(5 * time.Second)
    fmt.Println("Main goroutine exits")
}

				
			

Output :

				
					1
2
3
4
5
Main goroutine exits

				
			
  • We define a function printNumbers() that prints numbers from 1 to 5 with a delay of 1 second between each print statement.
  • In the main() function, we start a new goroutine go printNumbers() to execute printNumbers() concurrently.
  • We use time.Sleep() to keep the main goroutine alive for 5 seconds to allow the child goroutine to execute.
  • After 5 seconds, the main goroutine exits.

Channels:

				
					package main

import (
    "fmt"
)

func produceNumbers(numbers chan int) {
    defer close(numbers)
    for i := 1; i <= 5; i++ {
        numbers <- i
    }
}

func consumeNumbers(numbers chan int) {
    for num := range numbers {
        fmt.Println(num)
    }
}

func main() {
    numbers := make(chan int)
    go produceNumbers(numbers)
    consumeNumbers(numbers)
}

				
			

Output :

				
					1
2
3
4
5

				
			
  • We define two functions produceNumbers() and consumeNumbers() to respectively produce and consume numbers using a channel.
  • In produceNumbers(), we send numbers from 1 to 5 to the numbers channel using the <- operator.
  • In consumeNumbers(), we receive numbers from the numbers channel using the range keyword and print them.
  • In the main() function, we create a channel numbers using make(chan int) and start a goroutine to produce numbers concurrently.
  • We then call consumeNumbers() to consume and print the numbers from the channel.

In this chapter, we covered a wide range of operations in Go, from basic arithmetic to advanced concurrency operations using goroutines and channels. Understanding these operations is essential for writing efficient and effective Go programs. By mastering these concepts, you'll be well-equipped to tackle a variety of programming tasks in Go. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India