Operators: complete list
yourbasic.org/golang
Arithmetic
Operator | Name | Types |
---|---|---|
+ |
sum | integers, floats, complex values, strings |
- |
difference | integers, floats, complex values |
* |
product | |
/ |
quotient | |
% |
remainder | integers |
& |
bitwise AND | |
| |
bitwise OR | |
^ |
bitwise XOR | |
&^ |
bit clear (AND NOT) | |
<< |
left shift | integer << unsigned integer |
>> |
right shift | integer >> unsigned integer |
See Arithmetic operators in the Go language specification for complete definitions of the shift, quotient and remainder operators, integer overflow, and floating point behavior.
See Bitwise operators cheat sheet for more about how to manipulate bits with operators and functions in package math/bits (bitcount, rotate, reverse, leading and trailing zeros).
Comparison
Comparison operators compare two operands and yield an untyped boolean value.
Operator | Name | Types |
---|---|---|
== |
equal | comparable |
!= |
not equal | |
< |
less | integers, floats, strings |
<= |
less or equal | |
> |
greater | |
>= |
greater or equal |
- Boolean, integer, floats, complex values and strings are comparable.
- Strings are ordered lexically byte-wise.
- Two pointers are equal if they point to the same variable or if both are nil.
- Two channel values are equal if they were created by the same call to make or if both are nil.
- Two interface values are equal if they have identical dynamic types and equal concrete values or if both are nil.
- A value
x
of non-interface typeX
and a value t of interface typeT
are equal ift
’s dynamic type is identical toX
andt
’s concrete value is equal tox
. - Two struct values are equal if their corresponding non-blank fields are equal.
- Two array values are equal if their corresponding elements are equal.
Logical
Logical operators apply to boolean values. The right operand is evaluated conditionally.
Operator | Name | Description |
---|---|---|
&& |
conditional AND | p && q means "if p then q else false" |
|| |
conditional OR | p || q means "if p then true else q" |
! |
NOT | !p means "not p" |
Pointers and channels
Operator | Name | Description |
---|---|---|
& |
address of | &x generates a pointer to x |
* |
pointer indirection | *x denotes the variable pointed to by x |
<- |
receive | <-ch is the value received from channel ch |
Operator precedence
Go operator precedence spells MACAO
Unary operators
Unary operators have the highest priority and bind the strongest.
Binary operators (MACAO)
Priority | Operators | Note |
---|---|---|
1 | * / % << >> & &^ |
Multiplicative |
2 | + - | ^ |
Additive |
3 | == != < <= > >= |
Comparison |
4 | && |
And |
5 | || |
Or |
Binary operators of the same priority associate from left to right.
Statement operators
The ++
and - -
operators form statements and fall outside the operator hierarchy.
Examples
Expression | Evaluation order |
---|---|
x / y * z |
(x / y) * z |
*p++ |
(*p)++ |
^a >> b |
(^a) >> b |
1 + 2*a[i] |
1 + (2*a[i]) |
m == n+1 && <-ch > 0 |
(m == (n+1)) && ((<-ch) > 0) |