Defination
An Operator
in a programming language is a symbol that tell the compiler to perform specific mathematical, relation or logical operation and got the result.
Binary Operator || Unary Operator
A binary operator operation takes two operands and a unary operator operation takes only one operand.
Operators Precedence In Go
The following is the operator precedence in Go.
1 | * / % << >> & &^ // first |
Top ones have higher precedence. The operators in the same line have the same precedence. Of course ()
is used to promote precedence.
Operators in Go
Operators feature
Type | Operator | Name | Feature |
---|---|---|---|
Arithmetic_Binary_Basic | + | addition | Two Operands must be both values of the same numeric type. |
Arithmetic_Binary_Basic | - | subtraction | Same with above one |
Arithmetic_Binary_Basic | * | multiplication | Same with above one |
Arithmetic_Binary_Basic | / | division | Same with above one |
Arithmetic_Binary_Basic | % | remainder | Two Operands must be both values of the same integer type. |
Arithmetic_Binary_Bitwise | & | bitwise and | Same with above one |
Arithmetic_Binary_Bitwise | | | bitwise or | Same with above one |
Arithmetic_Binary_Bitwise | ^ | bitwise xor | Same with above one |
Arithmetic_Binary_Bitwise | &^ | bitwise clear | Same with above one |
Arithmetic_Binary_Bitwise | << | bitwise left shift | The left operand must be an integer and the right operand must be also an integer |
Arithmetic_Binary_Bitwise | >> | bitwise right shift | Same with above one |
Arithmetic_Unary | + | positive | +n is equivalent to 0 + n . |
Arithmetic_Unary | - | negative | -n is equivalent to 0 - n . |
Arithmetic_Unary | ^ | bitwise complement | ^n is equivalent to m ^ n , where m is a value all of which bits are 1. For example, if the type of n is int8 , then m is -1 , and if the type of n is uint8 , then m is 0xFF . |
String_Binary | + | string concatenation | The two operands must be both values of the same string type. |
Boolean_Binary | && | boolean and | The two operands must be both values of the same boolean type. |
Boolean_Binary | || | boolean or | Same with above one |
Boolean_Unary | ! | boolean not | The type of the only operand must be a boolean type. |
Comparsion_Binary | == | equal to | Generally, the types of its two operands must be the same |
Comparsion_Binary | != | not qeual to | Same with above one |
Comparsion_Binary | < | less thann | The two operands must be both values of the same integer type, floating-point type or string type. |
Comparsion_Binary | <= | less than or equal to | Same with above one |
Comparsion_Binary | > | larger than | Same with above one |
Comparsion_Binary | >= | larger than or equal to | Same with above one |
Arithmetic Operators Tips
There is some tips about arithmetic operators.
About OverFlows
Overflows are not allowed for typed constants but are allowed for non-constant and untyped constant, ether the values are intermediate or final results;
- Allowed:
- Untyped constant: the value wil not be truncated
- Non constant: the vlue will be truncated;
- Not Allowed:
- Typed constant
1 | package main |
About the results of arithmetic operator operations
Except bitwise shift operations, the result of a binary arithmetic operator operation
is a typed value of the same type of the two operands if the two operands are both typed values of the same type.
is a typed value of the same type of the typed operand if only one of the two operands is a typed value.
1
In the computation, the other (untyped) value will be deduced as a value of the type of the typed operand. In other words, the untyped operand will be implicitly converted to the type of the typed operand.
is still an untyped value if both of the two operands are untyped. The default type of the result value is one of the two default types and it is the one appears latter in this list:
int
,rune
,float64
,complex128
. For example, if the default type of one untyped operand isint
, and the other one isrune
, then the default type of the result untyped value isrune
.
1 | package main |
About integer division and remainder operations
Assume x, y
are two operands with the same integer type, the quotient q, x/y
and the remainder r , x % y
satisfy x = q * y + r
, where |r| < |y|
;
- the sign of
r
is always the same withx
; - the result of
q
it toword to zero.
1 | package main |
Using op=
for binary arithmetic operators
For a binary arithmetic operator op
, x = x op y
can be shortened to x op= y
. In the short form, x
will be only evaluated once.
1 | var a, b int8 = 3, 5 |