In Go, operators are symbols that perform operations on operands (variables or values). They are essential for performing calculations, comparisons, logical operations, and more. This tutorial will cover the basic types of operators available in Go, including arithmetic, comparison, logical, and bitwise operators.
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, and modulus.
+ : Addition- : Subtraction* : Multiplication/ : Division% : Modulus (remainder of division)Comparison operators are used to compare two values. They return a boolean result (true or false).
== : Equal to!= : Not equal to< : Less than> : Greater than<= : Less than or equal to>= : Greater than or equal toLogical operators are used to combine conditional statements. They return a boolean result.
&& : Logical AND (returns true if both operands are true)|| : Logical OR (returns true if at least one operand is true)! : Logical NOT (inverts the truth value of the operand)Bitwise operators perform operations on individual bits of a number. They are useful for low-level programming tasks.
& : Bitwise AND| : Bitwise OR^ : Bitwise XOR (exclusive OR)&^ : Bitwise AND NOT<< : Left shift>> : Right shiftLet's explore each type of operator with practical examples.
1package main23import "fmt"45func main() {6a := 107b := 389fmt.Println("Addition:", a + b) // Output: Addition: 1310fmt.Println("Subtraction:", a - b) // Output: Subtraction: 711fmt.Println("Multiplication:", a * b) // Output: Multiplication: 3012fmt.Println("Division:", a / b) // Output: Division: 313fmt.Println("Modulus:", a % b) // Output: Modulus: 114}
1package main23import "fmt"45func main() {6a := 107b := 2089fmt.Println("Equal to:", a == b) // Output: Equal to: false10fmt.Println("Not equal to:", a != b) // Output: Not equal to: true11fmt.Println("Less than:", a < b) // Output: Less than: true12fmt.Println("Greater than:", a > b) // Output: Greater than: false13fmt.Println("Less than or equal to:", a <= b) // Output: Less than or equal to: true14fmt.Println("Greater than or equal to:", a >= b) // Output: Greater than or equal to: false15}
1package main23import "fmt"45func main() {6x := true7y := false89fmt.Println("Logical AND:", x && y) // Output: Logical AND: false10fmt.Println("Logical OR:", x || y) // Output: Logical OR: true11fmt.Println("Logical NOT:", !x) // Output: Logical NOT: false12}
1package main23import "fmt"45func main() {6a := 10 // Binary: 10107b := 6 // Binary: 011089fmt.Println("Bitwise AND:", a & b) // Output: Bitwise AND: 2 (Binary: 0010)10fmt.Println("Bitwise OR:", a | b) // Output: Bitwise OR: 14 (Binary: 1110)11fmt.Println("Bitwise XOR:", a ^ b) // Output: Bitwise XOR: 12 (Binary: 1100)12fmt.Println("Bitwise AND NOT:", a &^ b) // Output: Bitwise AND NOT: 8 (Binary: 1000)13fmt.Println("Left shift:", a << 1) // Output: Left shift: 20 (Binary: 10100)14fmt.Println("Right shift:", a >> 1) // Output: Right shift: 5 (Binary: 0101)15}
Now that you have a good understanding of operators in Go, the next step is to learn about control structures such as loops and conditionals. These will allow you to write more complex programs by controlling the flow of execution based on conditions.
Stay tuned for our next tutorial on "Control Structures"!