Operators are the foundation of any programming language. Thus the functionality of the Go language is incomplete without the use of operators. Operators allow us to perform different kinds of operations on operands. In the Go language, operators Can be categorized based on their different functionality:
Arithmetic Operators
These are used to perform arithmetic/mathematical operations on operands in Go language:
- Addition: The ‘+’ operator adds two operands. For example, x+y.
- Subtraction: The ‘-‘ operator subtracts two operands. For example, x-y.
- Multiplication: The ‘*’ operator multiplies two operands. For example, x*y.
- Division: The ‘/’ operator divides the first operand by the second. For example, x/y.
- Modulus: The ‘%’ operator returns the remainder when the first operand is divided by the second. For example, x%y.
Note: -, +, !, &, *, <-, and ^ are also known as unary operators and the precedence of unary operators is higher. ++ and -- operators are from statements they are not expressions, so they are out from the operator hierarchy.
Example:
// Go program to illustrate the
// use of arithmetic operators
package main
import "fmt"
func main() {
p:= 34
q:= 20
// Addition
result1:= p + q
fmt.Printf("Result of p + q = %d", result1)
// Subtraction
result2:= p - q
fmt.Printf("\nResult of p - q = %d", result2)
// Multiplication
result3:= p * q
fmt.Printf("\nResult of p * q = %d", result3)
// Division
result4:= p / q
fmt.Printf("\nResult of p / q = %d", result4)
// Modulus
result5:= p % q
fmt.Printf("\nResult of p %% q = %d", result5)
}
Output:
Result of p + q = 54
Result of p - q = 14
Result of p * q = 680
Result of p / q = 1
Result of p % q = 14
Relational Operators
Relational operators are used for the comparison of two values. Let’s see them one by one:
- ‘=='(Equal To) operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise, it returns false. For example, 5==5 will return true.
- ‘!='(Not Equal To) operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise, it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.
- ‘>'(Greater Than)operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise, it returns false. For example, 6>5 will return true.
- ‘<‘(Less Than)operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise, it returns false. For example, 6<5 will return false.
- ‘>='(Greater Than Equal To)operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise, it returns false. For example, 5>=5 will return true.
- ‘<='(Less Than Equal To)operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise, it returns false. For example, 5<=5 will also return true.
Example:
// Go program to illustrate the
// use of relational operators
package main
import "fmt"
func main() {
p:= 34
q:= 20
// ‘=='(Equal To)
result1:= p == q
fmt.Println(result1)
// ‘!='(Not Equal To)
result2:= p != q
fmt.Println(result2)
// ‘<‘(Less Than)
result3:= p < q
fmt.Println(result3)
// ‘>'(Greater Than)
result4:= p > q
fmt.Println(result4)
// ‘>='(Greater Than Equal To)
result5:= p >= q
fmt.Println(result5)
// ‘<='(Less Than Equal To)
result6:= p <= q
fmt.Println(result6)
}
Output:
false
true
false
true
true
false
Logical Operators
They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration.
- Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).
- Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.
- Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.