Operators are used to perform mathematical or logical operations. Go provides several operators that can be categorized into various groups like arithmetic operators, comparison(relational) operators, assignment operators, logical operators, bitwise operators and miscellaneous operators.
Let's have a quick look at these operators and their uses.
Arithmetic Operators
These operators are used for performing mathematical operations like addition, subtraction, multiplication, division and modulus.
For those who are new to programming, modulus operators return the remainder of a division operation.
The code snippet below shows how to use arithmetic operators.
package main
import "fmt"
func main() {
var a int = 20
var b int = 30
// Addition
fmt.Println(a + b)
// prints 50
// Subtraction
fmt.Println(b - a)
// prints 10
// Multiplication
fmt.Println(a * b)
//prints 600
// Division
fmt.Println(b / a)
// prints 1, (logically it should be 1.5) reason is given below
// Modulus
fmt.Println(b % a)
// prints 10
}
All the results were as per expectation but the division returned the value 1.
The reason for this behavior is that, for the result to be a floating point number both the operands must be of a float type.
Comparison Operators
Comparison operators are also called as the relational operators. These operators are used to compare two values. Comparison operators include equality, inequality, less than, less than equal to, greater than and greater than equal to. These operations result in a boolean value i.e. either true or false.
The usage of these operators is demonstrated below.
package main
import fmt
func main(){
// Equality check
fmt.Println(a==b);
// results in false
// inequality check
fmt.Println(a!=b)
//results in true
// less than
fmt.Println(a<b)
// results in true
// less than or equal to
fmt.Println(a<=b)
//results in true
// greater than
fmt.Println(a>b)
// results in false
// greater than or equal to
fmt.Println(a>=b)
// results in false
}
Assignment operators
The assignment operator is used to assign value to a variable. Assignment operators can also be combined with operators for some common combination of operations like add and assign value( += ), subtract and assign value( -= ) and so on.
All of the assignment operators are listed below.
package main
import "fmt"
func main(){
var a float32 = 10
fmt.Println("Initial Value of a:",a)
// Add and assign
a += 5
// is equivalent to a = a + b
fmt.Println("Value of a after adding 5:",a)
// subtract and assign
a -= 5
// is equivalent to a = a - b
fmt.Println("Value of a after subtracting 5:",a)
// Multiply and assign
a *= 5
// is equivalent to a = a * 5
fmt.Println("Value of a after multiplying by 5:",a)
// Divide and assign
a /= 5
// is equivalent to a = a / 5
fmt.Println("Value of a after dividing by 5:",a)
}
Logical Operators
Logical operators are used to combine conditional expressions. AND, OR and NOT are logical operations with the following properties.
AND
Logical AND results in true
only when both conditions are true
, otherwise it results in a false.
<condition 1> && <condition 2>
Condition 1 | Condition2 | Result |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
OR
Logical OR results in false
only if both conditions are false
, otherwise it results in true
.
<condition 1> || <condition 2>
Condition 1 | Condition2 | Result |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
NOT
Logical NOT negates the value from true to false and vice versa
!<condition>
Condition | result |
true | false |
false | true |
Using logical AND (&&)
package main
import "fmt"
func main(){
var a int = 10
var b int = 15
// combining two conditions together
if a<20 && a>5{
fmt.Println("A is greater than 5 and less than 20")
}else{
fmt.Println("A is either less than 6 or A is greater than 19")
}
}
Using logical OR ( || )
package main
import "fmt"
func main(){
var a int = 10
var b int = 15
// combining two conditions together with ||
if a>20 || a<5{
fmt.Println("A is less than 5 or greater than 20")
}else{
fmt.Println("A is greater than 4 and A is less than 21")
}
}
Using logical NOT ( ! )
package main
import "fmt"
func main(){
var isActive bool = false
if !isActive{
fmt.Println(isActive)
isActive = !isActive
}
fmt.Println(!isActive)
}
Bitwise Operators
Bitwise operators perform operations at the bit level. Bitwise operations are faster than normal operations since they directly manipulate the bits. Bitwise operators include bitwise AND ( & ), bitwise OR ( | ), bitwise XOR ( ^ ), bitwise clear ( &^ ), left shift( << ) and right shift ( >> ).
Bitwise AND truth table
bit 1 | bit 2 | result bit |
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
Bitwise OR truth table
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Bitwise XOR truth table
1 | 1 | 0 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
Bitwise AND NOT (Bit clear &^) Operator
This operator is used to unset the specific bits. Observe the following example.
result = number1 &^ number2
Number 1 | 1 | 1 | 0 |
Number 2 | 0 | 1 | 0 |
Result | 1 | 0 | 0 |
Therefore, result = 1 0 0
In short, this operator takes the number2 and inverts its bits, then it performs the bitwise & on the number1 and the inverted number2.
The effect of this operation is that for every set bit of number2, the corresponding bit of number1 will be unset.
Shift Operators
Left shift ( << )
This operator shifts the bits of the operand by a specified number to the left and adds that many zeroes at the end of the number.
Example: shifting bits of 101110 to the left by 2
result = 101110<<2
result = 111000
n<<k is equivalent to n*2^k
Shifting bits of a number n to the left by k is equivalent to multiplying the number by 2^k
Right shift ( >> )
This operator shifts the bits of the operand by a specified number to the right and adds that many zeroes at the beginning of the number.
Example: shifting bits of 101110 to right by 2
result = 101110>>2
result = 001011
n>>k is equivalent to the floor of n/(2^k)
Shifting the bits of a number to the right by k is equivalent to the floor of the division of the number by 2^k.
Miscellaneous Operators
Address operator ( & )
if you put & in front of an addressable operand of type T, it will give you a pointer of type T to its address location. You can store or pass the address of a struct instance to a function.
Receiver operator ( <- )
This operator is used for passing values between concurrently running goroutines. In the upcoming Go concurrency-related posts I will discuss this operator.
References
To read more about Golang operators you can use the following.
Go specifications - Operators
Conclusion
This post was a quick overview of the operators in Go. Being aware of all the operators provided by a programming language is crucial to utilize all the features of the language. I hope this post will help you in your Go learning journey. Comment and share your learnings.