- Hands-On System Programming with Go
- Alex Guerrieri
- 272字
- 2021-06-24 13:42:21
Operations
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: