Exploring Go Operators

Exploring Go Operators

Introduction

In Go programming, operators serve as the building blocks for performing various operations on variables and values. They encompass a wide range of functionalities, from basic arithmetic to logical and bitwise operations. Let's delve into each category and explore the operators with examples.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations like addition, subtraction, multiplication, division, and modulus.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
-SubtractionSubtracts one value from anotherx - y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1x++
--DecrementDecreases the value of a variable by 1x--
package main
import "fmt"

func main() {
  var a = 15 + 25
  fmt.Println(a) // Output: 40
}
package main
import "fmt"

func main() {
  var b = 50 - 30
  fmt.Println(b) // Output: 20
}
package main
import "fmt"

func main() {
  var c = 10 * 5
  fmt.Println(c) // Output: 50
}
package main
import "fmt"

func main() {
  var d = 100 / 5
  fmt.Println(d) // Output: 20
}
package main
import "fmt"

func main() {
  var e = 25 % 7
  fmt.Println(e) // Output: 4
}

Assignment Operators

Assignment operators are used to assign values to variables. They include simple assignment (=) and compound assignment operators like +=, -= etc.

OperatorExampleSame As
\=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
\>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3
package main
import "fmt"

func main() {
  var x = 10
  x += 5
  fmt.Println(x) // Output: 15
}
package main
import "fmt"

func main() {
  var y = 20
  y -= 8
  fmt.Println(y) // Output: 12
}
package main
import "fmt"

func main() {
  var z = 5
  z *= 4
  fmt.Println(z) // Output: 20
}

Comparison Operators

Comparison operators are used to compare two values and return either true or false based on the comparison result.

OperatorNameExample
\==Equal tox == y
!=Not equalx != y
\>Greater thanx > y
<Less thanx < y
\>=Greater than or equal tox >= y
<=Less than or equal tox <= y
package main
import "fmt"

func main() {
  var x = 5
  var y = 3
  fmt.Println(x > y) // Output: true
}
package main
import "fmt"

func main() {
  var a = 10
  var b = 10
  fmt.Println(a == b) // Output: true
}

Logical Operators

Logical operators are used to determine the logic between variables or values. They include logical AND (&&), logical OR (||), and logical NOT (!).

OperatorNameDescriptionExample
&&Logical andReturns true if both statements are truex < 5 && x < 10
Logical orReturns true if one of the statements is truex < 5 &#124;&#124; x < 4
!Logical notReverse the result, returns false if the result is true!(x < 5 && x < 10)
package main
import "fmt"

func main() {
  var x = 5
  fmt.Println(x < 5 && x < 10) // Output: false
}
package main
import "fmt"

func main() {
  var y = 8
  fmt.Println(y < 10 || y > 15) // Output: true
}
package main
import "fmt"

func main() {
  var z = true
  fmt.Println(!z) // Output: false
}

Bitwise Operators

Bitwise operators are used on binary numbers to perform bit-level operations. They include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).

OperatorNameDescriptionExample
&ANDSets each bit to 1 if both bits are 1x & y
ORSets each bit to 1 if one of two bits is 1x &#124; y
^XORSets each bit to 1 if only one of two bits is 1x ^ b
<<Zero fill left shiftShift left by pushing zeros in from the rightx << 2
\>>Signed right shiftShift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall offx >> 2
package main
import "fmt"

func main() {
  var x = 5
  var y = 3
  fmt.Println(x & y) // Output: 1
}
package main
import "fmt"

func main() {
  var a = 5
  var b = 3
  fmt.Println(a | b) // Output: 7
}
package main
import "fmt"

func main() {
  var c = 5
  var d = 3
  fmt.Println(c ^ d) // Output: 6
}
package main
import "fmt"

func main() {
  var e = 8
  fmt.Println(e << 2) // Output: 32
}
package main
import "fmt"

func main() {
  var f = 8
  fmt.Println(f >> 2) // Output: 2
}

Understanding and utilizing these operators effectively is essential for performing various operations efficiently in Go programming.