Operator Precedence
Unary operators have the highest precedence.
As the ++ and -- operators form statements, not expressions, they fall outside the operator hierarchy.
Go supports only post-increment and post-decrement operators, not pre-increment and pre-decrement.
There are five precedence levels for binary operators.
Binary operators of the same precedence associate from left to right.
Precedence | Operator |
5 | * / % << >> & &^ |
4 | + - | ^ |
3 | == != < <= > >= |
2 | && |
1 | || |
Arithmetic Operators
Arithmetic operators apply to numeric values and yield a result of the same type as the first operand.
Operator | Description | Apply To |
+ | sum | integers, floats, complex values, strings |
- | difference | integers, floats, complex values |
* | product | integers, floats, complex values |
/ | quotient | integers, floats, complex values |
% | remainder | integers |
& | bitwise AND | integers |
| | bitwise OR | integers |
^ | bitwise XOR | integers |
&^ | bit clear (AND NOT) | integers |
<< | left shift | integers << unsigned integer |
>> | right shift | integers >> unsigned integer |
It can be combined with the assignment operator in short-form like +=, -=, etc.
The floating-point number follows the IEEE-754 standards.
Comparison Operators
Comparison operators compare two operands and yield an untyped boolean value.
Operator | Description |
== | equal |
!= | not equal |
< | less |
<= | less or equal |
> | greater |
>= | greater or equal |
Logical operators
Logical operators apply to boolean values and yield a result of the same type as the operands.
The right operand is evaluated conditionally.
Operator | Description |
&& | conditional AND |
|| | conditional OR |
! | NOT |
Address Operators
For an operand x of type T, the address operation &x generates a pointer of type *T to x.
If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.
If x is nil, an attempt to evaluate *x will cause a run-time panic.
Operator | Description |
& | Returns the address of a variable |
* | Pointer to a variable |
package main
import "fmt"
func main() {
i := 3
j := 7
p := &i
fmt.Println(*p)
// 3
*p = 21
fmt.Println(i)
// 21
p = &j
*p = *p / 2
fmt.Println(j)
// 3
}
As you can see in the above example, it looks similar to C language.
Receive Operator
For an operand ch of channel type, the value of the receive operation <-ch is the value received from the channel ch.
package main
import (
"fmt"
)
func sendDataToChannel(ch chan int, value int) {
ch <- value
}
func main() {
txValue := 7
var rxValue int
ch := make(chan int)
go sendDataToChannel(ch, txValue)
rxValue = <-ch
fmt.Println(rxValue)
// 7
}
Create a channel and communicate with it.
I'll post more detail about channels at the goroutines.
'Go' 카테고리의 다른 글
[Go] Constant (0) | 2021.10.05 |
---|---|
[Go] The Error of Floating Point Number (0) | 2021.10.04 |
[Go] File handling (0) | 2020.12.02 |
[Go] Configure VSCode (0) | 2020.11.28 |
[Go] User-Defined Type (0) | 2020.11.02 |
댓글