Go language operator

Posted by shaunmckinnon on Tue, 04 Jan 2022 09:23:13 +0100

Operators are used to perform mathematical or logical operations while the program is running.

The built-in operators of Go language are:

  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Bitwise Operators
  • Assignment Operators
  • Other Operators

Next, let's take a detailed look at the introduction of each operator.

Arithmetic operator

The following table lists the arithmetic operators for all Go languages. Assume A value of 10 and B value of 20.

The following example demonstrates the usage of each arithmetic operator:

example

package main

import "fmt"

func main() {

   var a int = 21
   var b int = 10
   var c int

   c = a + b
   fmt.Printf("first line - c The value of is %d\n", c )
   c = a - b
   fmt.Printf("Second line - c The value of is %d\n", c )
   c = a * b
   fmt.Printf("Third line - c The value of is %d\n", c )
   c = a / b
   fmt.Printf("Fourth line - c The value of is %d\n", c )
   c = a % b
   fmt.Printf("The fifth line - c The value of is %d\n", c )
   a++
   fmt.Printf("Line 6 - a The value of is %d\n", a )
   a=21   // For the convenience of testing, a is reassigned to 21 here
   a--
   fmt.Printf("Line 7 - a The value of is %d\n", a )
}

Operation results of the above examples:

first line - c The value of is 31
 Second line - c The value of is 11
 Third line - c The value of is 210
 Fourth line - c The value of is 2
 The fifth line - c The value of is 1
 Line 6 - a The value of is 22
 Line 7 - a The value of is 20

Relational operator

The following table lists the relational operators for all Go languages. Assume A value of 10 and B value of 20.

The following example demonstrates the use of relational operators:

example

package main

import "fmt"

func main() {
   var a int = 21
   var b int = 10

   if( a == b ) {
      fmt.Printf("first line - a be equal to b\n" )
   } else {
      fmt.Printf("first line - a Not equal to b\n" )
   }
   if ( a < b ) {
      fmt.Printf("Second line - a less than b\n" )
   } else {
      fmt.Printf("Second line - a Not less than b\n" )
   }
   
   if ( a > b ) {
      fmt.Printf("Third line - a greater than b\n" )
   } else {
      fmt.Printf("Third line - a Not greater than b\n" )
   }
   /* Lets change value of a and b */
   a = 5
   b = 20
   if ( a <= b ) {
      fmt.Printf("Fourth line - a Less than or equal to b\n" )
   }
   if ( b >= a ) {
      fmt.Printf("The fifth line - b Greater than or equal to a\n" )
   }
}

Operation results of the above examples:

first line - a Not equal to b
 Second line - a Not less than b
 Third line - a greater than b
 Fourth line - a Less than or equal to b
 The fifth line - b Greater than or equal to a

Logical operator

The following table lists the logical operators for all Go languages. Assume that A is True and B is False.

The following examples demonstrate the use of logical operators:
example

package main

import "fmt"

func main() {
   var a bool = true
   var b bool = false
   if ( a && b ) {
      fmt.Printf("first line - Condition is true\n" )
   }
   if ( a || b ) {
      fmt.Printf("Second line - Condition is true\n" )
   }
   /* Modify the values of a and b */
   a = false
   b = true
   if ( a && b ) {
      fmt.Printf("Third line - Condition is true\n" )
   } else {
      fmt.Printf("Third line - Condition is false\n" )
   }
   if ( !(a && b) ) {
      fmt.Printf("Fourth line - Condition is true\n" )
   }
}

Operation results of the above examples:

Second line - Condition is true
 Third line - Condition is false
 Fourth line - Condition is true

Bitwise Operators

Bit operator operates on the binary bits of an integer in memory.

The following table lists the calculations of the bitwise operators &, |, and ^:

Assume A = 60; B = 13; Its binary number is converted to:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

The bitwise operators supported by Go language are shown in the following table. Suppose A is 60 and B is 13:

The following example demonstrates the use of bitwise operators:

example

package main

import "fmt"

func main() {

   var a uint = 60      /* 60 = 0011 1100 */  
   var b uint = 13      /* 13 = 0000 1101 */
   var c uint = 0          

   c = a & b       /* 12 = 0000 1100 */
   fmt.Printf("first line - c The value of is %d\n", c )

   c = a | b       /* 61 = 0011 1101 */
   fmt.Printf("Second line - c The value of is %d\n", c )

   c = a ^ b       /* 49 = 0011 0001 */
   fmt.Printf("Third line - c The value of is %d\n", c )

   c = a << 2     /* 240 = 1111 0000 */
   fmt.Printf("Fourth line - c The value of is %d\n", c )

   c = a >> 2     /* 15 = 0000 1111 */
   fmt.Printf("The fifth line - c The value of is %d\n", c )
}

Operation results of the above examples:

first line - c The value of is 12
 Second line - c The value of is 61
 Third line - c The value of is 49
 Fourth line - c The value of is 240
 The fifth line - c The value of is 15

Assignment Operators

The following table lists the assignment operators for all Go languages.

The following example demonstrates the use of assignment operators:

example

package main

import "fmt"

func main() {
   var a int = 21
   var c int

   c =  a
   fmt.Printf("Line 1 - =  Operator instance, c Value is = %d\n", c )

   c +=  a
   fmt.Printf("Line 2 - += Operator instance, c Value is = %d\n", c )

   c -=  a
   fmt.Printf("Line 3 - -= Operator instance, c Value is = %d\n", c )

   c *=  a
   fmt.Printf("Line 4 - *= Operator instance, c Value is = %d\n", c )

   c /=  a
   fmt.Printf("Line 5 - /= Operator instance, c Value is = %d\n", c )

   c  = 200;

   c <<=  2
   fmt.Printf("Line 6  - <<= Operator instance, c Value is = %d\n", c )

   c >>=  2
   fmt.Printf("Line 7 - >>= Operator instance, c Value is = %d\n", c )

   c &=  2
   fmt.Printf("Line 8 - &= Operator instance, c Value is = %d\n", c )

   c ^=  2
   fmt.Printf("Line 9 - ^= Operator instance, c Value is = %d\n", c )

   c |=  2
   fmt.Printf("Line 10 - |= Operator instance, c Value is = %d\n", c )

}

Operation results of the above examples:

Line 1 - =  Operator instance, c Value is = 21
 Line 2 - += Operator instance, c Value is = 42
 Line 3 - -= Operator instance, c Value is = 21
 Line 4 - *= Operator instance, c Value is = 441
 Line 5 - /= Operator instance, c Value is = 21
 Line 6  - <<= Operator instance, c Value is = 800
 Line 7 - >>= Operator instance, c Value is = 200
 Line 8 - &= Operator instance, c Value is = 0
 Line 9 - ^= Operator instance, c Value is = 2
 Line 10 - |= Operator instance, c Value is = 2

Other Operators

The following table lists other operators for the Go language.

The following examples demonstrate the use of other operators:
example

package main

import "fmt"

func main() {
   var a int = 4
   var b int32
   var c float32
   var ptr *int

   /* Operator instance */
   fmt.Printf("Line 1 - a Variable type is = %T\n", a );
   fmt.Printf("Line 2 - b Variable type is = %T\n", b );
   fmt.Printf("Line 3 - c Variable type is = %T\n", c );

   /*  & And * operator instances */
   ptr = &a     /* 'ptr' Contains the address of the 'a' variable */
   fmt.Printf("a The value of is  %d\n", a);
   fmt.Printf("*ptr by %d\n", *ptr);
}

Operation results of the above examples:

Line 1 - a Variable type is = int
 Line 2 - b Variable type is = int32
 Line 3 - c Variable type is = float32
a The value of is 4
*ptr For 4

Operator priority

Some operators have higher priority, and the operation direction of binary operators is from left to right. The following table lists all operators and their priorities. From top to bottom, the priority is from high to low:

Of course, you can temporarily raise the overall operation priority of an expression by using parentheses.

Operation results of the above examples:
example

package main

import "fmt"

func main() {
   var a int = 20
   var b int = 10
   var c int = 15
   var d int = 5
   var e int;

   e = (a + b) * c / d;      // ( 30 * 15 ) / 5
   fmt.Printf("(a + b) * c / d The value of is : %d\n",  e );

   e = ((a + b) * c) / d;    // (30 * 15 ) / 5
   fmt.Printf("((a + b) * c) / d The value of is  : %d\n" ,  e );

   e = (a + b) * (c / d);   // (30) * (15/5)
   fmt.Printf("(a + b) * (c / d) The value of is  : %d\n",  e );

   e = a + (b * c) / d;     //  20 + (150/5)
   fmt.Printf("a + (b * c) / d The value of is  : %d\n" ,  e );  
}

Operation results of the above examples:

(a + b) * c / d The value of is : 90
((a + b) * c) / d The value of is  : 90
(a + b) * (c / d) The value of is  : 90
a + (b * c) / d The value of is  : 50