Operators in Golang


December 31, 2021, Learn eTutorial
1619

In this tutorial, you will learn various types of operators used in Go programming languages.
We will discuss further how these operators are used in creating expressions that make up the Go code simple and easy.

What are operators in Golang?

Operators are the basic component in every programming language. An operator in any programming language is a symbol that represents the performance of some mathematical calculations or functions. Operators are used to performing specific operations on operands (variables and values). An operand is one of the inputs or an argument taken by an operator. For example addition of 20 + 30 =?  is an expression composed of both operands and operators, the numbers 20 & 30 represent the input or operands while the symbols” + “, “=” forms operators?

GO : operators

Note: The expression is evaluated on the basis of precedence & associativity of operators

Operators can be classified into unary and binary types based on the operand's participation in an operation.

  •     Unary operator: - Operator that works with one operand.
  •     Binary operator:-Operator that works with two operands.

What are the types of operators in Golang?

Golang supports operators that are similar to other programming languages like 

  1.     Sign operator
  2.     Assignment operator
  3.     Increment & decrement operator
  4.     Arithmetic operator
  5.     Boolean operator
  6.     Comparison operator
  7.     Bitwise operator  

In the next few sections, we are going to discuss each operator separately.

GO : operators

Sign operator in Golang

Sign operator represents a sign of a value in the Go programming language. There are mainly two types of sign operators.

GO : operators

The positive (“+”) and negative (“-“) sign operators represent a sign of the integer value of a variable declared in a Golang. Let us understand with a simple Go program given below

Example


package main
import "fmt"

func main() {

 var s = 1
// -(-1)= +1
fmt.Println(" + sign operator prints positive numbers ")
 fmt.Println(-(-s))
// -1 = -1 
fmt.Println(" - sign operator prints negative numbers ")
   fmt.Println(-s)
}

Output:


+ sign operator prints positive numbers 
1
 - sign operator prints negative numbers 
-1

In the above program, a variable s is declared using the var keyword the minus sign indicates the sign of integer whether it is a positive number or negative number.

Increment and decrement operator

Go supports increment and decrement operations as in many other programming languages to either increase (increment) or decrease (decrement) a value. The symbol that denotes increment operator is “++ “& the Decrement operator is “--“. Suppose if a variable x is declared and assigned value 10.ie var x = 10.

GO : operators

Let us understand the concept with an example

Example


package main
import "fmt"

func main() {

    x := 10

    x++  //increment operator x=x+1=10+1=11
    x++    //increment operator  x=11+1=12

    fmt.Println("The varaiable x is incremented two times ",x)

    x--      // decrement  operator x=12,x-1=12-1=11
    fmt.Println("The variable x is decremented one time ", x)
}

Output:


The varaiable x is incremented two times  12
The variable x is decremented one time  11

Arithmetic operators

Arithmetic operators are used to performing arithmetic operations between the operands.

Operator Name of the Operation Description Examples
Operation Result
+ Addition Used to add two operands 2 + 5 7
- Subtraction Used to subtract the second operand from the first operand. 5 - 4 1
* Multiplication Used to multiply two operands 2 * 6 12
/ Division Used to divide the first operand by the second operand. 6 / 2 3
% Modulus This operator returns the remainder of the division between the operands. 5 % 2 1

Example programs using Arithmetic operators:


package main
import ("fmt")
func main() {
  x:= 10
  y:= 2
  fmt.Println("Addition:",x+y)
  fmt.Println("Subtraction:",x-y)
  fmt.Println("Multiplication:",x*y)
  fmt.Println("Division:",x/y)
  fmt.Println("Modulus:",x%y)
}

Output:


Addition: 12
Subtraction: 8
Multiplication:20
Division:5
Modulus:0

Comparison operators

Comparison operators compare its left-hand-side value (or expression) with the right-hand-side value (or expression). Comparison operators are also known as relational operators. The output of an operation using a comparison operator will be a Boolean value that is true or false.

Operator Name of the Operation Description Examples
Operation Result
== Equal This operator is used to check if two operands are equal or not. If the first operand is equal to the second operand, then it returns true. Otherwise, it returns false. 4 == 4 TRUE
2 == 5 FALSE
!= Not equal This operator will check if two operands are equal or not, and it will return true if they are not equal and false otherwise. 2 != 2 FALSE
4 != 5 TRUE
Less This operator will return true if the value of the first operand is lesser than the second operand and will return false otherwise. 3 < 6 TRUE
2 < 2 FALSE
<= Less or equal This operator will return true if the value of the first operand is lesser than or equal to the second operand and false otherwise. 7 <= 7 TRUE
10 <= 5 FALSE
Greater This operator will return true if the value of the first operand is greater than the second operand and will return false otherwise. 10 > 3 TRUE
3 > 3 FALSE
>= Greater or equal This operator will return true if the value of the first operand is greater than or equal to the second operand and false otherwise. 10 >= 10 TRUE
1 >= 4 FALSE

Example programs using Comparison operators:

  • Equal (==)

    Go program to check if the two numbers are equal or not

    
    package main
    import ("fmt")
    func main() {
    
      var x = 3
      var y = 2
    
      fmt.Println(x==y) 
    }
    
    

    Output:

    
    false
    
  • Not Equal (!=)

    Go program to check if the numbers are not equal

    
    package main
    import ("fmt")
    func main() {
    
      var x = 3
      var y = 2
    
      fmt.Println(x!=y) 
    }
    
    

    Output:

    
    true
    
  • Greater (>)

    Go program to check if ‘x’ is greater than ‘y’

    
    package main
    import ("fmt")
    func main() {
    
      var x = 3
      var y = 2
    
      fmt.Println(x>y) 
    }
    
    

    Output:

    
    true
    
  • Greater or equal (>=)

    Go program to check if ‘x’ is greater than or equal to ‘y’

    
    package main
    import ("fmt")
    func main() {
    
      var x = 3
      var y = 2
    
      fmt.Println(x>=y) 
    }
    
    

    Output:

    
    true
    
  • Less (<)

    Go program to check if ‘x’ is less than ‘y’

    
    package main
    import ("fmt")
    func main() {
      var x = 3
      var y = 2
      fmt.Println(x<y)}

    Output:

    
    false
    
  • Less or Equal (<=)

    Go program to check if ‘x’ is less than or equal to ‘y’

    
    packagemain
    import ("fmt")
    func main() {
    
      var x = 3
      var y = 2
    
      fmt.Println(x<=y) 
    }
    
    

    Output:

    
    false
    

Logical operators

Logical operators are used to performing the logical operations AND, OR, and NOT. The output of an operation using a logical operator will be a Boolean value that is true or false.

Operator Name of the Operation Description Examples
Operation Result
&& AND This operator returns true if both of the statements are true, and returns false otherwise. if the value of x is 5, (x > 4 &&  x < 6) true
if the value of x is 2, (x > 4 &&  x < 6) false
|| OR This operator returns true if any one of the statements is true, and returns false otherwise. if the value of x is 5, (x < 10 || x == 5) true
if the value of x is 5, (x > 10 || x == 20)   false
! NOT This operator reverses the result.  The NOT operator returns true if the statement is false, and it returns false if the statement is true. if the value of x is 5, (!(x > 4 &&  x < 6)) true
if the value of x is 5, (!(x > 4 ||  x < 6)) false

Example programs using Logical operators:

  • AND (&&)

    Go program to check if the value of ‘x’ is greater than 4 and lesser than 6

    
    package main
    import ("fmt")
    func main() {
      var x = 5
      fmt.Println(x > 4 &&  x < 6) 
    }
    
    

    Output:

    
    true
    
  • OR (||)

    Go program to check if the value of ‘x’ is lesser than 10 or greater than 20

    
    package main
    import ("fmt")
    func main() { 
    
      var x = 5
    
      fmt.Println(x < 10 || x > 20) 
    }
    
    

    Output:

    
    true
    
  • NOT (!)

    Go program to print ‘true’ if the value of ‘x’ is greater than 4

    
    package main
    import ("fmt")
    func main() {
    
      var x = 5
    
      fmt.Println(!(x > 4))
    }
    
    

    Output:

    
    false
    

BitWise operators

BitWise operators are used to performing bitwise operations on binary numbers. First, the integers are converted into binary format and then the operations are performed bit by bit.

Operator Name of the Operation Description Examples
& bitwise AND Both operands are converted into the binary number system and perform AND operation on every bit of the binary numbers. if x=3 and y=2, binary values of x = 11 and y = 10, then x & y = 10
| bitwise OR Both operands are converted into the binary number system and perform OR operations on every bit of the binary numbers. if x=3 and y=2, binary values of x = 11 and y = 10, then x | y = 11
^ bitwise XOR Both operands are converted into the binary number system and perform XOR operation on every bit of the binary numbers. if x=3 and y=2, binary values of x = 11 and y = 10, then x ^ y = 1
<<  left shift Both operands are converted into the binary number system and shift the bits of the first operand to the left by the specified number of bits. if x=10, binary values of x = 1010 then x << 2 = 101000
>>  right shift Both operands are converted into the binary number system and shift the bits of the first operand to the right by the specified number of bits. if x=10, binary values of x = 1010 then x >> 2 = 10
&^ AND NOT This operator is used to clear a bit. if x=5 and y=2, binary values of x = 101 and y = 10, then x &^ y = 101

Example programs using BitWise operators:

  • bitwise AND (&),bitwise OR (|),bitwise XOR (^)

    Example for bitwise AND, OR, XOR

    
    package main
    import ("fmt")
    func main() {
    
      var x = 10
      var y = 11
    
      fmt.Printf("x = %b\n",x)
      fmt.Printf("y = %b\n",y)
        
      fmt.Printf("x & y is %b\n",x & y)
      fmt.Printf("x | y is %b\n",x | y)
      fmt.Printf("x ^ y is %b\n",x ^ y)
    }
    
    

    Output:

    
    x = 11
    y = 10
    x & y is 10
    x | y is 11
    x ^ y is 1
    
  • left shift (<<)

    Example program

    
    package main
    import ("fmt")
    func main() {
    
      var x = 10
      fmt.Printf("x = %b\n",x)
    
      fmt.Printf("x << 2 is %b\n",x << 2)
    }
    
    

    Output:

    
    x = 1010
    x << 2 is 101000
    
  • right shift (>>)

    Example program

    
    package main
    import ("fmt")
    func main() {
    
      var x = 10
      fmt.Printf("x = %b\n",x)
     
      fmt.Printf("x >> 2 is %b\n",x >> 2)
    }
    
    

    Output:

    
    x = 1010
    x >> 2 is 10
    
  • AND NOT ($^)

    Example program

    
    package main
    import ("fmt")
    func main() {
    
      var x = 5
      var y = 2
    
      fmt.Printf("x = %b\n",x)
      fmt.Printf("y = %b\n",y)
    
      fmt.Printf("x &^ y = %b\n",x &^ y)
    }
    
    

    Output:

    
    x = 101
    y = 10
    x & y is 101
    

Assignment operators

A variable is a placeholder that stores values in it for later use in the Go program. These values are assigned to a variable by means of an operator called the assignment operator.

GO : operators

The var keyword assigns value 4 to variable a using operator “ = “.

NOTE: The shorthand declaration using the symbol “:= “in declaring variables a as a: = 4 is different from the assignment operator “= “.In a: = 4 the variable a is declared and assigned a value in a single step.

Operator Name of the Operation Description Examples
Operation Equivalent to
= Assignment operator This operator will assign the value of the right-hand-side operand to the left-hand-side operand. x = 2  
+= Add assignment operator This operator will perform the addition operation between the right-side and left-side operands, and the output will be assigned to the left-side operand. x += y x = x + y
-= subtract assignment operator This operator will subtract the right-side operand from the left-side operand, and the output will be assigned to the left-side operand. x -= y x = x-y
/= division assignment operator This operator will divide the left-side operand with the right-side operand, and the output will be assigned to the left-side operand. x /= y x = x/y
*= multiply assignment operator This operator will multiply the right-side operand with the left-side operand, and the output will be assigned to the left-side operand. x *= y x =x * y
%= modulus assignment operator First, this operator calculates the modulus on the left-side operand by the right-side operand and then assigns the output to the left-side operand. x %= y x = x%y
&= BitWise AND assignment operator First, this operator performs BitWise AND on the left-side operand by the right-side operand and then assigns the output to the left-side operand. x &= y x = x&y
|= BitWise Inclusive OR assignment operator First, this operator performs BitWise Inclusive OR on the left-side operand by the right-side operand and then assigns the output to the left-side operand. x |= y x = x|y
^= Bitwise Exclusive OR assignment operator First, this operator performs BitWise XOR on the left-side operand by the right-side operand and then assigns the output to the left-side operand. x ^= y x = x^y

Example programs using Assignment operators:


package main
import ("fmt")
func main() {

  var x = 2
  var a = 3
  var s = 3
  var m = 3
  var d = 3
  var md = 3
  a += 2
  s -=2
  m *= 2
  d /= 2
  md %= 2

  fmt.Println("Using Asignment operator :",x)
  fmt.Println("Using Add assignment operator :",a) 
  fmt.Println("Using Subtract assignment operator :",s) 
  fmt.Println("Using Multiply assignment operator :",m) 
  fmt.Println("Using Division assignment operator :",d) 
  fmt.Println("Using Modulus assignment operator :",md) 
}

Output:


Using asignment operator : 2
Using Add assignment operator : 5
Using Subtract assignment operator : 1
Using Multiply assignment operator : 6
Using Division assignment operator : 1
Using Modulus assignment operator  : 1

Example using BitWise AND assignment operator (&=)


package main
import ("fmt")
func main() {

  var x = 3
  var y = 2
  fmt.Printf("x = %b\n",x)
  fmt.Printf("y = %b\n",y)
 x &= y
  fmt.Printf("x &= y = %b\n",x)
}

Output:


x = 11
y = 10
x &= y = 10

Example using BitWise Inclusive OR assignment operator (|=)


package main
import ("fmt")
func main() {

  var x = 3
  var y = 2
  fmt.Printf("x = %b\n",x)
  fmt.Printf("y = %b\n",y)
 x |= y
  fmt.Printf("x |= y = %b\n",x)
}

Output:


x = 11
y = 10
x |= y = 11

Example using Bitwise Exclusive OR assignment operator (^=)


package main
import ("fmt")
func main() {

  var x = 5
  var y = 8
  fmt.Printf("x = %b\n",x)
  fmt.Printf("y = %b\n",y)
  x ^= y
  fmt.Printf("x ^= y = %b\n",x)
}

Output:


x = 101
y = 1000
x ^= y = 1101