Go Operators

Operators are symbols that allow us to perform various mathematical and logical operations on values and variables.

In Go, operators can be classified as below:

  • Arithmetic
  • Comparison
  • Logical
  • Bitwise
  • Assignment

Arithmetic Operators

The arithmetic operators can be used in the basic arithmetic operations on literal values or variables.

Operator Description Syntax
+ Addition. Adds two values x + y
- Subtraction. Subtracts the second operand from the first x – y
* Multiply x * y
/ Divide x / y
% Modulus. Gives the reminder after division x % y
++ Increment operator. Increments the value by 1 x ++
-- Decrement operator. Decreases the value by 1 x --

The following example demonstrates arithmetic operations such as addition, subtraction, and modulus.

Example: Arithmetic Operators
package main
import "fmt"

func main() {
		x := 5
	y := 4

    // Add x and y variables
    resultAdd := x + y
    fmt.Println(resultAdd) //output: 9

    //Subtract y from x
    resultSub := x - y
    fmt.Println(resultSub) // output: 1

    //Modulus of x and y
    resultMod := x % y
    fmt.Println(resultMod) // output: 1
}

Comparison Operators

Comparison operators are used to compare two literal values or variables.

Operator Description Syntax
== Equal x == y
!= Not Equal x != y
< Less than x < y
<= Less than or equal x <= y
> Greater than x > y
>= Greater than or equal x >= y

The following example demonstrates comparisons operators.

Example: Comparisons operators
package main
import "fmt"

func main() {
	x := 5
	y := 4

    fmt.Println (x == y) // output: false
    fmt.Println(x != y) // output: true
    fmt.Println (x < y) // output: false
    fmt.Println (x <= y) // output: false
    fmt.Println (x > y) // output: true
    fmt.Println (x >= y) // output: true
}

Logical Operators:

The logical operators are used to perform logical operations by combining two or more conditions. They return either true or false depending upon the conditions.

Operator Description Syntax
&& Logical AND. Returns true if both expressions are true expression1 && expression2
|| Logical OR. Returns true if any one of the expressions is true. exp1 || exp2
! Logical NOT. Returns true if an expression is false. Returns false if the expression is true. ! exp

The following example demonstrates the logical operators.

Example: Logical Operators
package main
import "fmt"

func main() {
	x ,y, z  := 5, 10, 20

	fmt.Println (x < y && x > z) //output: false
	fmt.Println(x < y || x < z) //output: true
}

Bitwise Operators

The bitwise operators work on bits and perform bit-by-bit operation.

Operator Description
& bitwise AND
| bitwise OR
^ bitwise XOR
<< left shift
>> Right shift

The following example demonstrates the bitwise operations.

Example: Bitwise Operators
package main
import "fmt"

func main() {

	var x, y = 3, 5
	var z int

	//Bitwise AND
	z = x & y
	fmt.Println("Bitwise AND ", z) // output: 1
	
    //Bitwise OR
	z = x | y
	fmt.Println("Bitwise OR ", z) //output: 7
	
    //Bitwise XOR
	z = x ^ y
	fmt.Println("Bitwise XOR ", z) //output: 6
}

In the above example, two int variables are x = 3 and y = 5. Binary of 3 is 0011 and 5 is 0101. so, x & y is 0001, which is numeric 1. x | y is 0111 which is numeric 7, and x ^ y is 0110 which is numeric 6.

Assignment Operators

The assignment operators are used to assign literal values or assign values by performing some arithmetic operation using arithmetic operators.

Operator Description Syntax
= Assign value x = y
+= Add and assign x += y same as x = x + y
-= Subtract and assign x -= y same as x = x - y
*= Multiply and assign x *= y same as x = x * y
/= Divide and assign x /= y same as x = x/y
%= Divide and assign modulus x %= y same as x = x%y

The following example demonstrates the assignment operators.

Example: Assignment Operators
package main
import "fmt"

func main() {
	x, y := 10, 20

	//Assign
	x = y
	fmt.Println(" = ", x) //output: 20

	// Add and assign
	x = 15
	x += y
	fmt.Println("+= ", x) //output: 35

	// Subtract and assign
	x = 25
	x -= y
	fmt.Println("-=", x) //output: 5

	//Multiply and assign
	x = 2
	x *= y
	fmt.Println("*=", x) //output: 40

	//Divide and assign
	x = 100
	x /= y
	fmt.Println("/=", x) // output: 5

	//Divide and assign modulus
	x = 30
	x %= y
	fmt.Println(" %= ", x) //output: 10
}