We already saw the assignment operation, which gives a new value to a variable using the = operator. Let's take a look at few more operators:
- There are the comparison operators, == and !=, which compare two values and return a Boolean.
- There are some mathematical operations that can be executed on all numerical variables of the same types, that is, +, -, *, and /. The sum operation is also used for concatenating strings. ++ and -- are shorthand for incrementing or decrementing a number by one. +=, -=, *=, and /= execute the operation before the equals sign between what's before and what's after the operator and assigns it to the variable on the left. These four operations produce a value of the same type of the variables involved; there are also other comparison operators that are specific to numbers: <, <=, >, and >=.
- Some operations are exclusive for integers and produce other integers: %, , &, |, ^, &^, <<, and >>.
- Others are just for Booleans and produce another Boolean: &&, ||, and !.
- One operator is channel only, <-, and it's used to receive values from or send them to a channel.
- For all non-pointer variables, it is also possible to use &, the reference operator, to obtain the variable address that can be assigned to a pointer variable. The * operator makes it possible to execute a dereference operation on a pointer and obtain the value of the variable indicated by it:
Operator | Name | Description | Example |
= | Assignment | Assigns the value to a variable | a = 10 |
:= | Declaration and assignment | Declares a variables and assigns a value to it | a := 0 |
== | Equals | Compares two variables, returns a Boolean if they are the same | a == b |
!= | Not equals | Compares two variables, returns a Boolean if they are different | a != b |
+ | Plus | Sum between the same numerical type | a + b |
- | Minus | Difference between the same numerical type | a - b |
* | Times | Multiplication between the same numerical type | a * b |
/ | Divided | Division between the same numerical type | a / b |
% | Modulo | Remainder after division of the same numerical type | a % b |
& | AND | Bit-wise AND | a & b |
&^ | Bit clear | Bit clear | a &^ b |
<< | Left shift | Bit shift to the left | a << b |
>> | Right shift | Bit shift to the right | a >> b |
&& | AND | Boolean AND | a && b |
|| | OR | Boolean OR | a || b |
! | NOT | Boolean NOT | !a |
<- | Receive | Receive from a channel | <-a |
-> | Send | Send to a channel | a <- b |
& | Reference | Returns the pointer to a variable | &a |
* | Dereference | Returns the content of a pointer | *a |