Operators in Go are symbols that perform various operations on values and variables. These include arithmetic, comparison, logical, and bit manipulation operators. In this article, we will explore each of these operators and how to use them in Go with examples.
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. Here are the arithmetic operators in Go:
+ // Addition - // Subtraction * // Multiplication / // Division % // Remainder (Modulus)
Here's an example of how to use arithmetic operators in Go:
a := 10 b := 5 c := a + b // 15 d := a - b // 5 e := a * b // 50 f := a / b // 2 g := a % b // 0
Comparison operators are used to comparing values and variables. They return a boolean value of true or false depending on the outcome of the comparison. Here are the comparison operators in Go:
== // Equal to != // Not equal to < // Less than > // Greater than <= // Less than or equal to >= // Greater than or equal to
Here's an example of how to use comparison operators in Go:
a := 10 b := 5 c := a == b // false d := a != b // true e := a < b // false f := a > b // true g := a <= b // false h := a >= b // true
Logical operators are used to combining multiple conditions into a single expression. They return a boolean value of true or false depending on the outcome of the evaluation. Here are the logical operators in Go:
&& // AND || // OR ! // NOT
Here's an example of how to use logical operators in Go:
a := true b := false c := a && b // false d := a || b // true e := !a // false
Bitwise operators are used to perform operations on individual bits of a value. They are commonly used in low-level programming and device driver development. Here are the bitwise operators in Go:
& // AND | // OR ^ // XOR << // Left shift >> // Right shift
Here's an example of how to use bitwise operators in Go:
a := 5 // 0101 b := 3 // 0011 c := a & b // 0001 (AND) d := a | b // 0111 (OR) e := a ^ b // 0110 (XOR) f := a << 1 // 1010 (Left shift) g := a >> 1 // 0010 (Right shift)
In conclusion, operators are an important part of the Go programming language, and they allow you to perform various operations on values and variables. By understanding how to use arithmetic, comparison, logical, and bitwise operators in Go, you can write more efficient and flexible code.
I hope this helps, you!!
More such articles:
If this article adds any value to you then please clap and comment.