Operator

Arithmetic

OperatorNameTypes
+sumintegers, floats, complex values, strings
-differenceintegers, floats, complex values
*productintegers, floats, complex values
/quotientintegers, floats, complex values
%remainderintegers
&bitwise ANDintegers
|bitwise ORintegers
^bitwise XORintegers
&^bit clear (AND NOT)integers
<<left shiftinteger << unsigned integer
>>right shiftinteger >> unsigned integer
caution

GO does provide implicit numberic conversions. (why) So the following code would not work:

x := 1
y := 1.0
c := x/y

Bitwise operators

OperationResultDescription
0011 & 01010001Bitwise AND
0011 | 01010111Bitwise OR
0011 ^ 01010110Bitwise XOR
^01011010Bitwise NOT (same as 1111 ^ 0101)
0011 &^ 01010010Bitclear (AND NOT)
00110101<<211010100Left shift
00110101<<10000000000No upper limit on shift count
00110101>>200001101Right shift

Comparison

OperatorNameTypes
==equalcomparable
!=not equalcomparable
<lessintegers, floats, strings
<=less or equalintegers, floats, strings
>greaterintegers, floats, strings
>=greater or equalintegers, floats, strings
  • 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 type X and a value t of interface type T are equal if t’s dynamic type is identical to X and t’s concrete value is equal to x.
  • 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

OperatorNameTypes
&&conditional ANDp && q means "if p then q else false"
||conditional ORp || q means "if p then true else q"
!NOT!p means "not p"

Pointers and channels

OperatorNameDescription
&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

Ref

Operators: complete list

Last updated on