Go - Operators

Go - Operators

Operators

Operators & Operands

We can define operators as symbols that helps us to perform specific mathematical and logical computations on operands

a + b

  • plus is operator

  • a and b is operands

Comparison Operators

  1. compare two operands and yield a Boolean value

  2. allow values of the same data type for comparisons

  3. common comparisons-

  • Does one string match another?

  • Are two number the same?

  • Is one number greater than another?

  • \== equal

  • != not equal

  • < less than

  • <= less than or equal to

  • > greater than

  • >= greater than or equal to

Equal to (==)

  1. return true when the values are equal

main.go

 package main
   import "fmt"

   func main() {
         var city string = "Kolkata"
         var city_2 string = "calcutta"
         fmt.Println(city == city_2)
   }

   >>> go run main.go
   false

not equal to (!=)

  1. return True when the values are not equal.

main.go

package main
   import "fmt"

   func main() {
         var city string = "Kolkata"
         var city_2 string = "calcutta"
         fmt.Println(city != city_2)
   }

   >>> go run main.go
   true

less than (<)

  1. returns True when the left operand is lesser than the right operand.

main.go

package main
   import "fmt"

   func main() {
         var a, b int = 5,10
         fmt.Println(a < b)
   }

   >>> go run main.go
   true

less than or equal to (<=)

  1. returns True when the left operand is lesser or equal to the right operand

main.go

package main
   import "fmt"

   func main() {
         var a, b int = 10, 10
         fmt.Println(a <= b)
   }

   >>> go run main.go
   true

greater than (>)

  1. returns true when the left operand is greater than the right operand.

main.go

 package main
   import "fmt"

   func main() {
         var a, b int = 20, 10
         fmt.Println(a > b)
   }

   >>> go run main.go
   true

greater than or equal to (>=)

  1. returns true when the left operand is greater or equal to the right operand.

main.go

 package main
   import "fmt"

   func main() {
         var a, b int = 20, 20
         fmt.Println(a >= b)
   }

   >>> go run main.go
   true

Arithmetic Operators

  1. Arithmetic operators are frequently combined with the comparison operator to create a Boolean statement. These operators are used to perform common arithmetic
    operations, such as addition, subtraction and multiplication.

  2. common comparisons -

    1. Does the sum of two numbers equal a particular value?

    2. Is the difference between two numbers lesser than a particular value?

addition(+)

  1. adds the left and right operands.

main.go

package main
   import "fmt"

   func main() {
         var a, b string = "foo", "bar"
         fmt.Println(a + b)
   }

   >>> go run main.go
   foobar

subtraction(-)

  1. subtracts the right operand from the left operand.

main.go

  package main
   import "fmt"

   func main() {
         var a, b float64 = 79.02, 75.66
         fmt.Println("%.2f",a - b)
   }

   >>> go run main.go
   3.36

multiplication (*)

  1. multiples both operands.

main.go

 package main
   import "fmt"

   func main() {
         var a, b int = 5,10
         fmt.Println(a * b)
   }

   >>> go run main.go
   50

division (/)

  1. returns the quotient when left operand is divided by right operand

main.go

  package main
   import "fmt"

   func main() {
         var a, b int = 10, 2
         fmt.Println(a / b)
   }

   >>> go run main.go
   5

modulus (%)

  1. returns the remainder when left operand is divided by right operand.

main.go

  package main
   import "fmt"

   func main() {
         var a, b int = 24, 7
         fmt.Println(a % b)
   }

   >>> go run main.go
   3

increment(++)

  1. unary operator are operators that act upon a single operand to produce a new value.

  2. This operator increments the value of the operands by one.

main.go

package main
   import "fmt"

   func main() {
         var i int = 1
         i++
         fmt.Println(i)
   }

   >>> go run main.go
   2

decrement (--)

  1. unary operator are operators that act upon a single operand to produce a new value.

  2. This operator decrement the value of the operands by one.

main.go

  package main
   import "fmt"

   func main() {
         var i int = 1
         i--
         fmt.Println(i)
   }

   >>> go run main.go
   0

Logical Operators

  1. Used to determine the logic between variables or values.

  2. common logical comparisons-

  3. Are two variables both true?

  4. Does either of two expressions evaluate to true?

And (&&)

  1. returns true if both the statements are true.

  2. returns false when either of the statement is false.

main.go

 package main
   import "fmt"

   func main() {
         var x int = 10
         fmt.Println((x < 100) && (x < 200))
         fmt.Println((x < 300) && (x < 0))
         i++
         fmt.Println(i)
   }

   >>> go run main.go
   true
   false

OR (||)

  1. returns true if one of the statement is true.

  2. returns false when both statements are false.

main.go

   package main
   import "fmt"

    func main() {
         var x int = 10
         fmt.Println((x < 0) || (x < 200))
         fmt.Println((x < 0) || (x > 200))
         i++
         fmt.Println(i)
   }
   >>> go run main.go
   true
   false

NOT (!)

  1. unary operator.

  2. Reverses the result, returns false if the expression evaluates to true and vice versa.

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 10, 20
         fmt.Println(!(x > y))
         fmt.Println(!(true))
         fmt.Println(!(false))
         i++
         fmt.Println(i)
   }
   >>> go run main.go
   true
   false
   true

Assignment Operators.

assign (=)

  1. assign left operand with the value to the right

  2. x = y

main.go

 package main
   import "fmt"

    func main() {
         var x int = 10
         var y int
         y = x
         fmt.Println(y)
   }
   >>> go run main.go
   10

add and assign (+=)

  1. assign left operand with the addition result.

  2. x+= y means x = x + y

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 10, 20
         x +=y
         fmt.Println(x)
   }
   >>> go run main.go
   30

subtract and assign (-=)

  1. assign left operand with the subtraction result.

  2. x-= y means x = x - y

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 10, 20
         x -= y
         fmt.Println(x)
   }
   >>> go run main.go
   -10

multiply and assign (*=)

  1. assign left operand with the multiplication result.

  2. x*= y means x = x * y

main.go

 package main
   import "fmt"

    func main() {
         var x, y int = 10, 20
         x *= y
         fmt.Println(x)
   }
   >>> go run main.go
   200

divide and assign (/=)

  1. assign left operand with the quotient of the division.

  2. x/= y means x = x / y

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 200, 20
         x /= y
         fmt.Println(x)
   }
   >>> go run main.go
   10

divide and assign modulus (%=)

  1. assign left operand with the remainder of the division.

  2. x%= y means x = x % y

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 210, 20
         x %= y
         fmt.Println(x)
   }
   >>> go run main.go
   10

Bitwise Operators

Bitwise operators work at bit level or perform bit by bit operation.

bitwise AND (&)

  1. takes two numbers as operands and does AND on every bit of two numbers.

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 12, 25
         z := x & y
         fmt.Println(z)
   }
   >>> go run main.go
   8

bitwise OR (|)

  1. takes two numbers as operands and does OR on every bit of two numbers.

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 12, 25
         z := x | y
         fmt.Println(z)
   }
   >>> go run main.go
   29

bitwise XOR (^)

  1. takes two numbers as operands and does XOR on every bit of two numbers.

  2. The result of XOR is 1 if the two bits are opposite

main.go

   package main
   import "fmt"

    func main() {
         var x, y int = 12, 25
         z := x ^ y
         fmt.Println(z)
   }
   >>> go run main.go
   21

right shift (>>)

  1. Shifts all bits towards left by a certain number of specified bits.

  2. The bits positions that have been vacated by the left shift operator are filled with 0.

main.go

   package main
   import "fmt"

    func main() {
         var x int = 212
         z := x << 1
         fmt.Println(z)
   }
   >>> go run main.go
   424

left shift (<<)

  1. Shifts all bits towards right by a certain number of specified bits.

  2. excess bits shifted off to the right are discarded.

main.go

   package main
   import "fmt"

    func main() {
         var x int = 212
         z := x >> 2
         fmt.Println(z)
   }
   >>> go run main.go
   53

That's great if you have make till here you have covered basic of Golang Operators

If you liked what you read, do follow and any feedback for further improvement will be highly appreciated!

Thank you and Happy Learning!👏