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.
Go supports all the basic arithmetic operations: addition, subtraction, multiplication, division, and modulus. Let’s explore each operation with examples:
package main
import "fmt"
func main() {
result := 10 + 5
fmt.Println("Result of addition:", result) // Output: Result of addition: 15
}
result
and assign it the sum of 10 and 5.fmt.Println()
function is used to print the result to the console.
package main
import "fmt"
func main() {
result := 10 - 5
fmt.Println("Result of subtraction:", result) // Output: Result of subtraction: 5
}
result
and assign it the difference of 10 and 5.
package main
import "fmt"
func main() {
result := 10 * 5
fmt.Println("Result of multiplication:", result) // Output: Result of multiplication: 50
}
result
and assign it the product of 10 and 5.
package main
import "fmt"
func main() {
result := 10 / 5
fmt.Println("Result of division:", result) // Output: Result of division: 2
}
result
and assign it the quotient of 10 divided by 5.
package main
import "fmt"
func main() {
result := 10 % 3
fmt.Println("Result of modulus:", result) // Output: Result of modulus: 1
}
We declare a variable result
and assign it the remainder of 10 divided by 3
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:
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
}
fmt.Println()
.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
}
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:
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.
package main
import "fmt"
func main() {
result := 10 | 5
fmt.Println("Result of bitwise OR:", result) // Output: Result of bitwise OR: 15
}
result
and assign it the result of performing a bitwise OR operation between 10 and 5.
package main
import "fmt"
func main() {
result := 10 ^ 5
fmt.Println("Result of bitwise XOR:", result) // Output: Result of bitwise XOR: 15
}
result
and assign it the result of performing a bitwise XOR operation between 10 and 5.
package main
import "fmt"
func main() {
result := 10 << 2
fmt.Println("Result of left shift:", result) // Output: Result of left shift: 40
}
result
and assign it the result of left shifting the binary representation of 10 by 2 positions.
package main
import "fmt"
func main() {
result := 10 >> 2
fmt.Println("Result of right shift:", result) // Output: Result of right shift: 2
}
result
and assign it the result of right shifting the binary representation of 10 by 2 positions.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:
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")
}
1
2
3
4
5
Main goroutine exits
printNumbers()
that prints numbers from 1 to 5 with a delay of 1 second between each print statement.main()
function, we start a new goroutine go printNumbers()
to execute printNumbers()
concurrently.time.Sleep()
to keep the main goroutine alive for 5 seconds to allow the child goroutine to execute.
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)
}
1
2
3
4
5
produceNumbers()
and consumeNumbers()
to respectively produce and consume numbers using a channel.produceNumbers()
, we send numbers from 1 to 5 to the numbers
channel using the <-
operator.consumeNumbers()
, we receive numbers from the numbers
channel using the range
keyword and print them.main()
function, we create a channel numbers
using make(chan int)
and start a goroutine to produce numbers concurrently.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 !❤️