Operators in Go
Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. In the Go programming language, also known as Golang, operators play a vital role in constructing expressions and statements. This article will cover the basic operators in Go and how to use them.
Types of Operators in Go
There are mainly five types of operators in Go:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
Let's dive into each of these operators in detail.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication etc. Here are the basic arithmetic operators in Go:
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulus)++
(Increment)--
(Decrement)
Here is an example of how to use these operators:
var a int = 9
var b int = 4
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)
a++
fmt.Println("Increment: ", a)
b--
fmt.Println("Decrement: ", b)
Relational Operators
Relational operators are used to compare two values. The result of these operations is either true or false. Here are the relational operators in Go:
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
Here is an example of how to use these operators:
var a int = 9
var b int = 4
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 than or equal to: ", a >= b)
fmt.Println("Less than or equal to: ", a <= b)
Logical Operators
Logical operators are used to combine two or more conditions. They return true or false based on the state of the conditions. Here are the logical operators in Go:
&&
(Logical AND)||
(Logical OR)!
(Logical NOT)
Here is an example of how to use these operators:
var a bool = true
var b bool = false
fmt.Println("AND: ", a && b)
fmt.Println("OR: ", a || b)
fmt.Println("NOT: ", !a)
Bitwise Operators
Bitwise operators are used to perform operations on binary representations of numbers. Here are the bitwise operators in Go:
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)<<
(Left shift)>>
(Right shift)
Here is an example of how to use these operators:
var a uint = 60
var b uint = 13
fmt.Println("Bitwise AND: ", a & b)
fmt.Println("Bitwise OR: ", a | b)
fmt.Println("Bitwise XOR: ", a ^ b)
fmt.Println("Left shift: ", a << 2)
fmt.Println("Right shift: ", a >> 2)
Assignment Operators
Assignment operators are used to assign values to variables. Here are the assignment operators in Go:
=
(Assign)+=
(Add and assign)-=
(Subtract and assign)*=
(Multiply and assign)/=
(Divide and assign)%=
(Modulus and assign)<<=
(Left shift and assign)>>=
(Right shift and assign)&=
(Bitwise AND and assign)^=
(Bitwise XOR and assign)|=
(Bitwise OR and assign)
Here is an example of how to use these operators:
var a int = 9
a += 1
fmt.Println("Add and assign: ", a)
a -= 1
fmt.Println("Subtract and assign: ", a)
a *= 2
fmt.Println("Multiply and assign: ", a)
a /= 2
fmt.Println("Divide and assign: ", a)
a %= 2
fmt.Println("Modulus and assign: ", a)
a <<= 1
fmt.Println("Left shift and assign: ", a)
a >>= 1
fmt.Println("Right shift and assign: ", a)
a &= 1
fmt.Println("Bitwise AND and assign: ", a)
a ^= 1
fmt.Println("Bitwise XOR and assign: ", a)
a |= 1
fmt.Println("Bitwise OR and assign: ", a)
Conclusion
In this tutorial, we have learned about the different types of operators in Go. These operators are essential tools when creating expressions and statements in your Go programs. Make sure to understand how each operator works as they form the basis of many programming tasks. Happy Coding!