AppDividend
Latest Code Tutorials

Golang Operator Example | Operators In Go Tutorial

0

Golang operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Go language has rich inbuilt operators and provides the following types of operators. Golang operators are the foundation of any programming language. The functionality of the Go language is incomplete without the use of operators. Operators allow us to perform the various kinds of operations on operands. In Go language, operators can be classified based upon their different functionality.

Golang Operator

Operators are tokens that tell the compiler to perform particular operations that may be mathematical, relational, or logical. The Golang language has inbuilt operators that can be grouped into the following categories.

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Bitwise Operators
  5. Misc Operators

Arithmetic Operators

These are used to perform Arithmetic operators on operands in Golang.

Operator Description Example
+ Adds two operands X + Y gives 21
Subtracts second operand from the first X – Y gives -11
* Multiplies both operands X * Y gives 100
/ Divides the numerator by the denominator. Y / X gives 29
% Modulus operator gives a remainder after the integer division. Y % X gives 0
++ Increment operator. It increases the integer value by one. X++ gives 11
Decrement operator. It decreases the integer value by one. X– gives 90

The -, +, !, &, *, <-, 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.

See the following code example.

// hello.go

package main

import "fmt"

func main() {
	var p int = 23
	var q int = 60
	fmt.Println(p + q)
}

Output

go run hello.go
40

Relational or Comparison Operators

The following table lists all the comparison operators supported by the Golang. Assume variable X holds 11, and variable Y holds 21.

Operator Description Example
== It checks if the values of two operands are equal or not; if yes, then the condition becomes true. (X == Y) is not true.
!= It checks if the values of two operands are equal or not; if not equal, then the condition becomes true. (X != Y) is true.
> It checks if the value of the left operand is greater than the value of the right operand; if yes, then the condition becomes true. (X > Y) is not true.
< It checks if the value of the left operand is less than the value of the right operand; if yes, the condition becomes true. (X < Y) is true.
>= It checks if the value of the left operand is greater than or equal to the value of the right operand; if yes, the condition becomes true. (X >= Y) is not true.
<= It checks if the value of the left operand is less than or equal to the value of the right operand; if yes, the condition becomes true. (X <= Y) is true.

See the following code example of Relational operators.

// hello.go

package main

import "fmt"

func main() {
	var k int = 21
	var b int = 19

	if k != b && k <= b {
		fmt.Println("1st True")
	}

	if k != b || k <= b {
		fmt.Println("2nd True")
	}

	if !(k == b) {
		fmt.Println("3rd True")
	}

}

Output

go run hello.go
2nd True
3rd True

Key points

  1. Boolean, integer, floats, complex values, and strings are equal.
  2. Strings are ordered lexically byte-wise.
  3. Two pointers are equal if they point to the same variable or if both are nil.
  4. Two-channel values are equal if they were built by the same call to make or if both are nil.
  5. Two interface values are equal if they have identical dynamic types and equal concrete values or if both are nil.

Logical Operators

Logical operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition into consideration.

  1. Logical AND: The ‘&&’ operator returns boolean True when both 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).
  2. Logical OR: The ‘|| operator returns True when one or both of the conditions in consideration are 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.
  3. Logical NOT: The ‘!’(NOT) operator returns True, if 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.

See the following code example of Logical operators.

// hello.go

package main

import "fmt"

func main() {
	var x int = 11
	var y int = 21

	if x != y && x >= y {
		fmt.Println("1st True")
	}

	if x != y || x <= y {
		fmt.Println("2nd True")
	}

}

Output

go run hello.go
2nd True

Bitwise Operators

Bitwise operators work on bits and perform the bit-by-bit operation.

The truth tables for &, |, and ^ are as follows.

  1. & (bitwise AND): It takes two numbers as operands and does AND on every bit of the two numbers. The output of AND is 1 only if both bits are 1.
  2. | (bitwise OR): It takes two numbers as operands and does OR on every bit of two numbers. The output of OR is 1 any of the two bits is 1.
  3. ^ (bitwise XOR): It takes two numbers as operands and does XOR on every bit of two numbers. The output of XOR is 1 if the two bits are different.
  4. << (left shift): It takes two numbers, left shifts the bits of the first operand, the second operand decides a number of places to shift.
  5. >> (right shift): It takes two numbers, right shifts the bits of the first operand, the second operand decides a number of places to shift.
  6. &^ (AND NOT): The &^ is a bit, clear operator.

See the following code example of a bitwise operator in Go.

// hello.go

package main

import "fmt"

func main() {
	p := 19
	k := 21

	// & (bitwise AND)
	op1 := p & k
	fmt.Printf("Result of p & k = %d", op1)

	// | (bitwise OR)
	op2 := p | k
	fmt.Printf("\nResult of p | k = %d", op2)

	// ^ (bitwise XOR)
	op3 := p ^ k
	fmt.Printf("\nResult of p ^ k = %d", op3)

	// << (left shift)
	op4 := p << 1
	fmt.Printf("\nResult of p << 1 = %d", op4)

	// >> (right shift)
	op5 := p >> 1
	fmt.Printf("\nResult of p >> 1 = %d", op5)

	// &^ (AND NOT)
	op6 := p &^ k
	fmt.Printf("\nResult of p &^ k = %d", op6)

}

Output

go run hello.go
Result of p & k = 17
Result of p | k = 23
Result of p ^ k = 6
Result of p << 1 = 38
Result of p >> 1 = 9
Result of p &^ k = 2

Assignment Operators

See the following assignment operators.

Operator Description Example
= It is a simple assignment operator, Assigns values from right side operands to left side operand. Z = X + Y will assign value of X + Y into Z
+= It adds AND assignment operator, It adds right operand to the left operand and assigns the result to left operand. Z += X is equivalent to Z = X + Y
-= It subtracts AND assignment operator, It subtracts right operand from the left operand and assigns the result to left operand. Z -= X is equivalent to Z = Z – X
*= It multiplies AND assignment operator, It multiplies right operand with the left operand and assigns the result to the left operand. Z *= X is equivalent to Z = Z * X
/= It divides AND assignment operator, It divides left operand with the right operand and assigns the result to left operand. Z /= X is equivalent to Z = Z / X
%= Modulus AND assignment operator, It takes modulus using two operands and assigns the result to the left operand. Z %= X is equivalent to Z = Z % X
<<= Left shift AND assignment operator. Z <<= 2 is same as Z = Z << 2
>>= Right shift AND assignment operator. Z >>= 2 is same as Z = Z >> 2
&= Bitwise AND assignment operator. Z &= Z is same as Z = Z & 2
^= Bitwise exclusive OR and assignment operator. Z ^= 2 is same as Z = Z ^ 2
|= Bitwise inclusive OR and assignment operator. Z |= 2 is same as Z = Z | 2

See the following code example of Assignment Operators.

// hello.go

package main

import "fmt"

func main() {
	var x int = 19
	var y int = 21

	// “=”(Simple Assignment)
	x = y
	fmt.Println(x)

	// “+=”(Add Assignment)
	x += y
	fmt.Println(x)

	//“-=”(Subtract Assignment)
	x -= y
	fmt.Println(x)

	// “*=”(Multiply Assignment)
	x *= y
	fmt.Println(x)

	// “/=”(Division Assignment)
	x /= y
	fmt.Println(x)

	// “%=”(Modulus Assignment)
	x %= y
	fmt.Println(x)
}

Output

go run hello.go
21
42
21
441
21

Miscellaneous Operators

See the following operators.

  1. &: This operator returns the address of the variable.
  2. *: This operator provides a pointer to a variable.
  3. <-:The name of this operator is received. It is used to receive a value from the channel.

See the following code example of Miscellaneous operators.

// hello.go

package main

import "fmt"

func main() {
	x := 9
	y := &x

	fmt.Println(*y)

	*y = 11
	fmt.Println(x)
}

Output

go run hello.go
9
11

Conclusion

Operators are one of the building blocks of any programming language. That is why the complete introduction of the Go language is incomplete without the use of operators. We have seen Arithmetic, logical, comparison, bitwise, assignment, and miscellaneous operator.

Finally, the Golang operator example is over.

See also

Identifiers in Golang

Keywords in Golang

Golang Variables

Golang Constants

Introduction to Golang

Installing Golang on MacOS

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.