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
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
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
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
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
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.