Golang provides different operators to perform different kinds of operations. The main operators are arithmetic, comparison,logical, assignment and bitwise operators.
Overview
Operators specify the operation we want to perform on elements. These elements are also called operands on which the operation is being performed. There are five main types of operators.
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on operands.
| Operator | Description |
|---|---|
+ | Addition |
- | Subtraction |
* | Multiplication of two numbers |
/ | Division. This returns float result for the division of two numbers. |
% | This returns the remainder for the division of two numbers. This is also called modulo operator |
++ | Unary increment by 1 |
-- | Unary decrement by 1 |
Note: Please note that those unary operators can be used only as postfix operators. Other languages like C, C++, Java, support both postfix and prefix operators which can be used as
a++or++a. Go does not support prefix unary operators.
1package main
2
3import "fmt"
4
5func main() {
6
7 a, b := 2, 5
8 fmt.Printf("%d + %d: %d\n", a, b, a+b)
9
10 fmt.Printf("%d - %d: %d\n", b, a, b-a)
11 fmt.Printf("%d * %d: %d\n", a, b, a*b)
12 fmt.Printf("%d / %d: %d\n", b, a, b/a)
13 // '/' returns quotient of division operation
14 fmt.Printf("%d mod %d: %d\n", b, a, b%a)
15 // '%' returns remainder of division operation
16
17 // unary operators
18 i := 10
19 i++
20 fmt.Printf("After i++ : %d\n", i)
21 i--
22 fmt.Printf("After i-- : %d\n", i)
23}
Output
2 + 5: 7
5 - 2: 3
2 * 5: 10
5 / 2: 2
5 mod 2: 1
After i++ : 11
After i-- : 10
Note: Go does not support implicit type conversion. If we want compiler to convert the type, we have to specify that explicitly. Other languages like C, C++, Java, support implicit type conversion (coersion), but not Go.
This means that we cannot mix types in these operators. For example, we cannot perform addition of two variables which are of types int and int32 or int and a byte or int and float. Below operations give compile error.
1func main() {
2 var aInt int = 10
3 var aInt32 int32 = 10
4 var aFloat32 float32 = 10.2
5 var aFloat64 float64 = 10.2
6 var aByte byte = 5
7 fmt.Println("aInt + aInt32:", aInt + aInt32)
8 fmt.Println("aInt + aByte:", aInt + aByte)
9 fmt.Println("aInt + aFloat32:", aInt + aFloat32)
10 fmt.Println("aFloat32 + aFloat64:", aFloat32 + aFloat64)
11}
The solution is to do type conversion explicitly to the required data type before performing operations.
The order of these operators depend on type of operations. If we combine multiple operations in single statement, we have to be careful. For example, in below snippet, we cannot easily imagine whether a + b will happen first or b * c. The order of operations are defined by PEMDAS if you ever encounter such code.
result = a + b * c
Ideally, you should avoid writing such statements to be clear about what you intend to do. So, above statement can be written as result = (a + b) * c if we intededed to perform this operation.
Comparison/Relational Operators
These are used to compare two values or variables.
| Operator | Description |
|---|---|
> | Greater than |
< | Less than |
<= | Less than or equal to |
>= | Greater than or equal to |
!= | Is not Equal to |
== | Equal to |
1package main
2
3import "fmt"
4
5func main() {
6 a, b := 10, 10
7 fmt.Printf("%d == %d: %t\n", a, b, a == b)
8 fmt.Printf("%d != %d: %t\n", a, b, a != b)
9
10 a = 20
11 fmt.Printf("%d > %d: %t\n", a, b, a > b)
12 fmt.Printf("%d < %d: %t\n", a, b, a < b)
13
14 fmt.Printf("%d >= %d: %t\n", a, b, a >= b)
15 fmt.Printf("%d <= %d: %t\n", a, b, a <= b)
16
17 a = 10
18 fmt.Printf("%d > %d: %t\n", a, b, a > b)
19 fmt.Printf("%d < %d: %t\n", a, b, a < b)
20
21 fmt.Printf("%d >= %d: %t\n", a, b, a >= b)
22 fmt.Printf("%d <= %d: %t\n", a, b, a <= b)
23
24}
Output
10 == 10: true
10 != 10: false
20 > 10: true
20 < 10: false
20 >= 10: true
20 <= 10: false
10 > 10: false
10 < 10: false
10 >= 10: true
10 <= 10: true
Logical Operators
Logical operators are usually used with conditions to verify more than one condition or to negate a condition. These has three main operators.
| Operator | Description |
|---|---|
and | AND operation |
or | OR operation |
not | Complement operation |
1package main
2
3import "fmt"
4
5func main() {
6 i := 10
7
8 fmt.Printf("(i < 100) && (i > 5): %t\n", (i < 100) && (i > 5)) // true
9 fmt.Printf("(i < 100) && (i > 500): %t\n", (i < 100) && (i > 500)) // false
10
11 fmt.Printf("(i > 100) && (i > 500): %t\n", (i < 100) && (i > 5)) // false
12 fmt.Printf("(i < 100) && (i > 500): %t\n", (i < 100) && (i > 500)) // true
13
14 // unary operator - reverses logical output
15 fmt.Printf("!true: %t\n", !true)
16 fmt.Printf("!false: %t\n", !false)
17 fmt.Printf("!(i > 100): %t\n", !(i > 100))
18}
Output
(i < 100) && (i > 5): true
(i < 100) && (i > 500): false
(i > 100) && (i > 500): true
(i < 100) && (i > 500): false
!true: false
!false: true
!(i > 100): true
Assignment Operators
Assignment operators are used to assign values to a variable. These include very basic = assignment but include some more.
| Operator | Description |
|---|---|
= | Assign a value to a variable |
+= | Add and assigne a value |
-= | Subtract and assgn a value |
*= | Multiply and assign |
/= | Divide and assign a value |
//= | Divide and assign quotient |
%= | Divide and assign remainder |
1package main
2
3import "fmt"
4
5func main() {
6 var a int
7 a = 10
8
9 // short hand operator
10 b := 10
11
12 a += 5
13 fmt.Println("a += 5:", a)
14
15 b -= 2
16 fmt.Println("b -= 2:", b)
17
18 b *= 2
19 fmt.Println("b *= 2:", b)
20
21 b /= 2
22 fmt.Println("b /= 2:", b)
23
24 a %= 2
25 fmt.Println("a %= 2:", a)
26}
Output
a += 5: 15
b -= 2: 8
b *= 2: 16
b /= 2: 8
a %= 2: 1
Bitwise Operators
Bitwise operators perform operations at bit-level. All values in computer system are stored at bit level using 0 and 1.
| Operator | Description |
|---|---|
& | Bitwise AND operation |
| ` | ` |
^ | Bitwise XOR operation |
~ | Bitwise NOT operation |
<< | Shift Left operation |
>> | Shift Right operation |
1package main
2
3import "fmt"
4
5func main() {
6 // Perform bit by bit operations
7 a := 4 // 0100
8 b := 2 // 0010
9 c := 5 // 0101
10 fmt.Printf("%d & %d = %d\n", a, b, a&b) // 0000 = 0
11
12 fmt.Printf("%d | %d = %d\n", a, b, a|b) // 1010 = 6
13
14 fmt.Printf("%d ^ %d = %d\n", a, c, a^c) // 0001 = 1
15
16 fmt.Printf("%d << 1 = %d\n", b, b<<1) // 0100 = 4
17 fmt.Printf("%d << 2 = %d\n", b, b<<2) // 1000 = 8
18 fmt.Printf("%d << 1 = %d\n", c, c<<1) // 1010 = 10
19
20 fmt.Printf("%d >> 1 = %d\n", c, c>>1) // 0010 = 2
21}
Output
4 & 2 = 0
4 | 2 = 6
4 ^ 5 = 1
2 << 1 = 4
2 << 2 = 8
5 << 1 = 10
5 >> 1 = 2
Conclusion
In this tutorial, we looked at various kinds of operations in Golang. We have five main operator types in Go. These are arithmetic operators, bitwise operators, comparison operators, logical operators and assignment operators.


Comments