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
compare two operands and yield a Boolean value
allow values of the same data type for comparisons
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 (==)
- 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 (!=)
- 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 (<)
- 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 (<=)
- 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 (>)
- 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 (>=)
- 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
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.common comparisons -
Does the sum of two numbers equal a particular value?
Is the difference between two numbers lesser than a particular value?
addition(+)
- 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(-)
- 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 (*)
- 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 (/)
- 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 (%)
- 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(++)
unary operator are operators that act upon a single operand to produce a new value.
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 (--)
unary operator are operators that act upon a single operand to produce a new value.
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
Used to determine the logic between variables or values.
common logical comparisons-
Are two variables both true?
Does either of two expressions evaluate to true?
And (&&)
returns true if both the statements are true.
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 (||)
returns true if one of the statement is true.
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 (!)
unary operator.
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 (=)
assign left operand with the value to the right
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 (+=)
assign left operand with the addition result.
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 (-=)
assign left operand with the subtraction result.
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 (*=)
assign left operand with the multiplication result.
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 (/=)
assign left operand with the quotient of the division.
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 (%=)
assign left operand with the remainder of the division.
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 (&)
- 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 (|)
- 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 (^)
takes two numbers as operands and does XOR on every bit of two numbers.
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 (>>)
Shifts all bits towards left by a certain number of specified bits.
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 (<<)
Shifts all bits towards right by a certain number of specified bits.
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!👏