Go Operators
An Operator is a symbol that is used to perform logical or mathematical tasks. Go provides us different types of built-in operators, these mainly are:
-
Arithmetic Operators
-
Assignment Operators
-
Relational Operators
-
Logical Operators
-
Bitwise Operators
In this tutorial, we will explore all the built-in operators that Go provides us. Let's explore them one by one.
Arithmetic Operators
Arithmetic operators are those operators that are used to perform basic arithmetic operations like addition, subtraction, division, etc. In Go, the common arithmetic operators work on both the integer and float data types. The arithmetic operators can also be split into two subcategories, and these are:
-
Binary Operators
-
Unary Operators
Binary Operators
The binary operators are those that involve two operands. In Go, we have four binary operators, and these are:
Operator |
Symbol |
Addition Operator |
+ |
Division Operator |
/ |
Modulus Operator |
% |
Multiplication Operator |
* |
Example: Addition Operator
The addition operator is denoted by the symbol + and is used to perform the addition operation in Go. This operator is mainly used with numbers but it also works with strings.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
var b int = 3
fmt.Println(a + b)
var firstName string = "study"
var secondName string = "tonight"
fmt.Println(firstName + secondName)
}
5
studytonight
Example: Divison Operator
The division Operator is used when we want to perform some dividing operations and the symbol for the division operator is /. It should be noted that the division operator's result is floored, for example, if we do something like 4 / 3 the answer we will get is 1 instead of 1.3333...
Also, if we divide anything with 0, it will cause the program to crash, and a run-time panic occurs. Also, division with 0.0 wich floating-point numbers gives an infinite result: +Inf
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
var b int = 3
fmt.Println(a/b)
fmt.Println(b/a)
}
0
1
Example: Modulus Operator
The modulus operator is used when we want to extract the remainder of a division operation. The symbol for the modulus operator is %.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
var b int = 3
fmt.Println(a%b)
fmt.Println(b%a)
}
2
1
Example: Multiplication Operator
The multiplication operator is used when we want to perform simple multiplication of two operands. The symbol used for the multiplication operator in Go is *.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
var b int = 3
fmt.Println(a * b)
}
6
Assignment Operators
In Programming, the assignment operators are the operators that are used to assign a value to a variable. The most basic assignment operator is denoted by the = symbol. The value on the left of the = symbol is known as the left operand the value on the right of the = symbol is known as the right operand. In Go, we have different assignment operators available, these are mentioned in the table shown below.
Operator |
Symbol |
Simple Assignment |
= |
Add Assignment |
+= |
Subtract Assignment |
-= |
Multiply Assignment |
*= |
Division Assignment |
/= |
Modulus Assignment |
%= |
Bitwise AND Assignment |
&= |
Bitwise Exclusive OR(XOR) |
^= |
Bitwise Inclusive OR |
|= |
It should be noted that all these assignment operators that include two symbols in the format += or -=, simply mean that we are using the arithmetic operator and the simple assignment operator at the same time. In simple mathematical sense, a += 2 is the same as a = a + 2.
Now that we know about the different types of assignment operators let's make use of all these operators in a Go example.
Consider the example shown below
package main
import (
"fmt"
)
func main() {
var a, b int
a, b = 10, 4
a = b
fmt.Println("a now is:", a)
a += b
fmt.Println("a now is:", a)
a -= b
fmt.Println("a now is:", a)
a *= b
fmt.Println("a now is:", a)
a /= b
fmt.Println("a now is:", a)
a %= b
fmt.Println("a now is:", a)
a /= b
fmt.Println("a now is:", a)
a ^= b
fmt.Println("a now is:", a)
a |= b
fmt.Println("a now is:", a)
}
a now is: 4
a now is: 8
a now is: 4
a now is: 16
a now is: 4
a now is: 0
a now is: 0
a now is: 4
a now is: 4
Unary Operators
A unary operator is an operator that is used on a single operand only.
There are only two unary operators that come under the category of the arithmetic operators and these are:
-
Increment Operator
-
Decrement Operator
Increment Operator
The purpose of the increment operator is to increase the value of the operand by one. The symbol used to represent the increment operator is ++.
It should be noted that both the unary operators can be applied after the operand(number) and not before. Go doesn't support anything like ++operand or --operand.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
a++
fmt.Println(a)
}
3
Decrement Operator
The purpose of the increment operator is to decrease the value of the operand by one. The symbol used to represent the increment operator is ++.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
a--
fmt.Println(a)
}
1
Another key point to note that the unary operators cannot be used as expressions in Go. You might have seen something like f(a++) in other programming languages, where f() is a call to a function and we are passing a unary operator as an argument, this is not valid in Go and hence cannot be used.
Consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
fmt.Println(a++)
}
./prog.go:9:15: syntax error: unexpected ++, expecting comma or )
Relational Operators
There are different relational operators that are present in Go, these are:
Operator |
Symbol |
Equality Operator |
== |
Not-Equal Operator |
!= |
Less-than Operator |
< |
Greater-than Operator |
> |
Less-then equal-to Operator |
<= |
Greater-then equal-to Operator |
>= |
Let's explore all the operators with the help of different tables that represents how they work and lastly we will see a code example of all of them.
Go is very strict about the values that are compared. It demands that values have to be of the same type. If one of them is a constant, it must be of a type compatible with the other. If these conditions are not satisfied, one of the values has first to be converted to the other’s type. <, <=, >, >=, == and != not only work on number types but also on strings.
They are called logical because the result value of these operators is of type bool.
Now let's explore all these operators in a Go program, consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a int = 2
var b int = 5
fmt.Println(a == b)
fmt.Println(a != b)
fmt.Println(a < b)
fmt.Println(a > b)
fmt.Println(a <= b)
fmt.Println(a >= b)
}
false
true
true
false
true
false
Logical Operators
Boolean constants and variables can also be combined with logical operators to produce a boolean value. Such a logical statement is not a complete Go statement in itself. Go has three boolean logical operators: AND and OR is binary operators where NOT is a unary operator. The && and || operators behave in a shortcut way that when the value of the left side is known, and it is sufficient to deduce the value of the whole expression.
Let's make use of the boolean logical operators in an example, consider the example shown below:
package main
import (
"fmt"
)
func main() {
var a bool = true
var b bool = false
fmt.Println(a && b)
fmt.Println(a || b)
fmt.Println(!a)
}
false
true
false
Bitwise Operators
They work only on integer variables having bit-patterns of equal length. %b is the format string for bit-representations.
The table shown below mentions the different types of bitwise operators that are present in Go.
Operator |
Symbol |
Bitwise AND Operator |
& |
Bitwise OR Operator |
| |
Bitwise XOR Operator |
^ |
Bit CLEAR Operator |
&^ |
Bitwise COMPLEMENT Operator |
^ |
Out of all the bitwise operators that are mentioned above, the Bitwise AND, OR, XOR and CLEAR are binary operators which means they require two operands to work on. However, the COMPLEMENT operator is a unary operator.
Now let's consider an example where we will make use of these bitwise operators in a Go program,
Consider the example shown below:
package main
import (
"fmt"
)
const (
x = iota
y
)
func main() {
fmt.Println(x & y)
fmt.Println(x | y)
fmt.Println(x ^ y)
fmt.Println(x &^ y)
fmt.Println(^x)
}
0
1
1
0
-1
Operator Precedence in Go
In Programming, the concept of operator precedence is used to determine the grouping of terms in an expression. It basically affects how an expression is evaluated. There are some operators that will have higher precedence than others, for example, the division operator has higher precedence over the addition operator.
A table is shown below, that depicts how the associativity of different operators is evaluated in Go.
Category |
Operator |
Associativity |
Postfix |
() [] -> . ++ -- |
Left to Right |
Unary |
+ - ! ~ ++ -- (type)* sizeof & |
Right to Left |
Multiplicative |
* / % |
Left to Right |
Additive |
+ - |
Left to Right |
Shift |
<< >> |
Left to Right |
Relational |
< <= >= > |
Left to Right |
AND |
& |
Left to Right |
XOR |
^ |
Left to Right |
OR |
| |
Left to Right |
Logical AND |
&& |
Left to Right |
Logical OR |
|| |
Left to Right |
Assignment |
= += -= *= /= %= &= |= ^= |
Right to Left |
Comma |
, |
Left to Right |
Conclusion
In the above article, we learned bout the different types of operators that are present in Go, from simple arithmetic operators to a little complicated logical operator and to the most complex bitwise operators. We also learned about their use cases and saw different examples of each of these operators.