Tutorialsteacher

Follow Us

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.

OperatorDescriptionSyntax
+Addition. Adds two valuesx + y
-Subtraction. Subtracts the second operand from the firstx – y
*Multiplyx * y
/Dividex / y
%Modulus. Gives the reminder after divisionx % y
++Increment operator. Increments the value by 1x ++
--Decrement operator. Decreases the value by 1x --

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.

OperatorDescriptionSyntax
==Equalx == y
!=Not Equalx != y
<Less thanx < y
<=Less than or equalx <= y
>Greater thanx > y
>=Greater than or equalx >= 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 &lt; y) // output: false
    fmt.Println (x <= y) // output: false
    fmt.Println (x > y) // output: true
    fmt.Println (x &gt;= 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.

OperatorDescriptionSyntax
&&Logical AND. Returns true if both expressions are trueexpression1 && 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 &lt; y || x < z) //output: true
}

Bitwise Operators

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

OperatorDescription
&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.

OperatorDescriptionSyntax
=Assign valuex = y
+=Add and assignx += y same as x = x + y
-=Subtract and assignx -= y same as x = x - y
*=Multiply and assignx *= y same as x = x * y
/=Divide and assignx /= y same as x = x/y
%=Divide and assign modulusx %= 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
}