Basics
Go Operators
Go Operators
Go operators include arithmetic and logical with strict type rules.
Introduction to Go Operators
In Go, operators are special symbols or keywords that perform operations on operands. Go has several types of operators including arithmetic, relational, logical, bitwise, assignment, and others. Each operator works with specific data types and follows strict type rules, ensuring type safety and reducing errors.
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, and division. Go supports the following arithmetic operators:
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus
package main
import "fmt"
func main() {
a := 10
b := 5
fmt.Println("Addition:", a + b) // 15
fmt.Println("Subtraction:", a - b) // 5
fmt.Println("Multiplication:", a * b) // 50
fmt.Println("Division:", a / b) // 2
fmt.Println("Modulus:", a % b) // 0
}
Relational Operators
Relational operators compare two values and return a boolean result: either true
or false
. They are crucial in control flow, such as in loops and conditional statements. The relational operators in Go include:
==
: Equal to!=
: Not equal to<
: Less than<=
: Less than or equal to>
: Greater than>=
: Greater than or equal to
package main
import "fmt"
func main() {
x := 10
y := 20
fmt.Println("x == y:", x == y) // false
fmt.Println("x != y:", x != y) // true
fmt.Println("x < y:", x < y) // true
fmt.Println("x <= y:", x <= y) // true
fmt.Println("x > y:", x > y) // false
fmt.Println("x >= y:", x >= y) // false
}
Logical Operators
Logical operators are used to combine two or more conditions and return a boolean result. They are most commonly used in conjunction with relational operators. The logical operators in Go are:
&&
: Logical AND||
: Logical OR!
: Logical NOT
package main
import "fmt"
func main() {
a := true
b := false
fmt.Println("a && b:", a && b) // false
fmt.Println("a || b:", a || b) // true
fmt.Println("!a:", !a) // false
}
Bitwise Operators
Bitwise operators perform operations on bits and are used in low-level programming. They work on integer types and perform bit-by-bit operations. The bitwise operators in Go include:
&
: AND|
: OR^
: XOR&^
: AND NOT<<
: Left shift>>
: Right shift
package main
import "fmt"
func main() {
x := 6 // 0110 in binary
y := 3 // 0011 in binary
fmt.Println("x & y:", x & y) // 2 (0010)
fmt.Println("x | y:", x | y) // 7 (0111)
fmt.Println("x ^ y:", x ^ y) // 5 (0101)
fmt.Println("x &^ y:", x &^ y) // 4 (0100)
fmt.Println("x << 1:", x << 1) // 12 (1100)
fmt.Println("x >> 1:", x >> 1) // 3 (0011)
}
Assignment Operators
Assignment operators are used to assign values to variables. In addition to the basic =
operator, Go provides compound assignment operators that combine arithmetic operations with assignment. These include:
+=
: Add and assign-=
: Subtract and assign*=
: Multiply and assign/=
: Divide and assign%=
: Modulus and assign&=
: Bitwise AND and assign|=
: Bitwise OR and assign^=
: Bitwise XOR and assign<<=
: Left shift and assign>>=
: Right shift and assign
package main
import "fmt"
func main() {
a := 5
a += 3
fmt.Println("a += 3:", a) // 8
a -= 2
fmt.Println("a -= 2:", a) // 6
a *= 2
fmt.Println("a *= 2:", a) // 12
a /= 3
fmt.Println("a /= 3:", a) // 4
a %= 3
fmt.Println("a %= 3:", a) // 1
}
Conclusion
Go's operators provide a rich set of functionalities for performing operations on data. Understanding these operators is fundamental to writing effective Go programs. Remember that Go enforces strict type rules, so operators work on compatible data types only. In the next post, we'll explore control flow using If Else statements.