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.
Operator | Name | Description | Example |
+ | Addition | Adds together two values | x + y |
- | Subtraction | Subtracts one value from another | x - y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | x++ |
-- | Decrement | Decreases the value of a variable by 1 | x-- |
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.
Operator | Example | Same As | |
\= | x = 5 | x = 5 | |
+= | x += 3 | x = x + 3 | |
-= | x -= 3 | x = x - 3 | |
*= | x *= 3 | x = x * 3 | |
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 | |
&= | x &= 3 | x = x & 3 | |
= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 | |
\>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = 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.
Operator | Name | Example |
\== | Equal to | x == y |
!= | Not equal | x != y |
\> | Greater than | x > y |
< | Less than | x < y |
\>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= 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 (!
).
Operator | Name | Description | Example | ||
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 | ||
Logical or | Returns true if one of the statements is true | x < 5 || x < 4 | |||
! | Logical not | Reverse 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 (>>
).
Operator | Name | Description | Example | |
& | AND | Sets each bit to 1 if both bits are 1 | x & y | |
OR | Sets each bit to 1 if one of two bits is 1 | x | y | ||
^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ b | |
<< | Zero fill left shift | Shift left by pushing zeros in from the right | x << 2 | |
\>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off | x >> 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.