## Go language operators

Go language operator priority describes the order in which operations are performed when the computer calculates an expression. Operations with higher priority are executed first, and then operations with lower priority are executed. For example, we often say that multiplication and division are executed first, and then addition and subtraction operations are executed.

## 1. Go language arithmetic operators

The following table shows all the arithmetic operators supported by the Go language. Assume that the value of variable

`A`

is`10`

and the value of variable`B`

is`20`

, then:

Operator | Description | Example |
---|---|---|

`+` | Add two operands | A+B=30 |

`-` | Subtract the second operand from the first | A-B=10 |

`*` | Multiply the two operands | A*B=200 |

`/` | Divide the numerator by the denominator | B/A=2 |

`%` | Modulo operator, and remainder of integer division | B%A=0 |

`++` | Increment operator, increase integer value by one | A++=11 |

`--` | Subtraction operator, decrease integer value by one | A—=9 |

## 2. Go language Relational Operators

The following table shows all the relational operators supported by Go language. Suppose the value of variable

`A`

is`10`

and the value of variable`B`

is`20`

, then:

Operator | Description | Example |
---|---|---|

`==` | Checks if the values of two operands are equal, if equal, then the condition becomes true. | (A==B) result is false |

`!=` | Checks if the values of two operands are equal or not, if the values are not equal, then the condition becomes true. | (A!=B) result is true |

`>` | Checks if the value of left operand is greater than the value of right operand, if yes, then the condition becomes true. | (A>B) result is false |

`<` | Checks if the value of left operand is less than the value of right operand, if yes, then the condition becomes true. | (A<B) Result is true |

`>=` | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A>=B) Result is false |

`<=` | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A<=B) Result is true |

## 3. Go language logical operators

The following table shows all the logical operators supported by the Go language. Assume that the value of variable

`A`

is`1`

and the value of variable`B`

is`0`

, then:

Operator | Description | Example |
---|---|---|

`&&` | Logical `AND` operator. If both the operands are non-zero, then the condition becomes true. | (A&&B) Result is true |

`||` | Logical`OR` operator. If any of the two operands is non-zero, then the condition becomes true. | (A||B) Result is true |

`!` | Logical NOT operator. Used to invert the logical state of its operands. If condition is true, then logical NOT operator will become false. | !(A&&B) Result is true |

## 4. Go language bitwise operators

Bitwise operators operate on bits and perform bit-by-bit operations. The truth tables for

`&`

,`|`

and`^`

are as follows:

>

p | q | p&q | p|q | p^q |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

Assume `A = 60`

, `B = 13`

; the current binary format is as follows:

A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011

The bitwise operators supported by Go are listed in the table below. Assume variable `A=60`

and variable `B=13`

, then:

Operator | Description | Example |
---|---|---|

& | If the binary `AND` operator exists in both operands, it is copied to the result. | (`A&B` ) results in `12` , which is `0000 1100` |

| | The binary `OR` operator copies a bit if it is present in either operand. | (A|B) results in 61, which is `0011 1101` |

^ | The binary `XOR` operator copies a bit if it is set in one operand but not in both. | (A^B) results in `49` , which is `0011 0001` |

<< | Binary left shift operator. The left operand value is shifted left by the number of bits specified by the right operand. | `A<<2` results in `240` , which is `1111 0000` |

>> | Binary right shift operator. The left operand value is shifted right by the number of bits specified by the right operand. | `A>>2` The result is 15, which is `0000 1111` |

## V. Go Language Assignment Operators

Go language supports the following assignment operators:

Operator | Description | Example |
---|---|---|

= | Simple assignment operator, assigns the value from the right operand to the left operand | `C=A+B` , that is, assigning the value of `A+B` to `C` |

`+=` | Addition and assignment operator, adds the right operand to the left operand and assigns the result to the left operand | `C+=A` is equivalent to `C=C+A` |

-= | Subtraction and assignment operator, subtracts the right operand from the left operand and assigns the result to the left operand | `C-=A` is equivalent to `C=C-A` |

*= | Multiplication and assignment operator, it multiplies the right operand with the left operand and assigns the result to the left operand | `C*=A` is equivalent to `C=C*A` |

/= | Division and assignment operator, it divides the left operand by the right operand and assigns the result to the left operand | `C/=A` is equivalent to `C=C/A` |

%= | Modulo and assignment operator, it takes the modulus of two operands and assigns the result to the left operand | `C%=A` is equivalent to `C=C%A` |

<<= | Left shift and assignment operator | `C<<=2` is equivalent to `C=C<<2` |

>>= | Right shift and assignment operator | `C>>=2` is equivalent to `C=C>>2` |

&= | Bitwise AND and assignment operator | `C&=2` is equivalent to `C=C&2` |

^= | Bitwise XOR and assignment operator | `C^=2` is equivalent to `C=C^2` |

|= | Bitwise inclusive `OR` and assignment operator | C|=2 is equivalent to C=C|2 |

## 6. Other operators in Go

There are some other important operators including

`sizeof`

and`? :`

, which are also supported in Go.

Operator | Description | Example |
---|---|---|

& | Return the address of a variable | `＆a` will give the actual address of the variable `a` . |

* | Pointer to a variable | `*a` is a pointer to the variable `a` . |

Operator precedence determines the grouping in an expression. This affects how the expression is evaluated. Some operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

When operators of the same level appear in the same expression, they are evaluated from left to right. For example, if multiplication and division are together, the multiplication and division operators are evaluated from left to right regardless of whether the multiplication operator comes first or the division operator comes first. The same is true for addition and subtraction.

For example: `x = 7 + 3 * 2`

; Here, the result of the calculation `x`

is assigned `13`

, not `20`

, because the operator `*`

has a higher precedence than `+`

, so it first multiplies `3 * 2`

and then adds `7`

.

Here, the operator with the highest precedence is placed at the top of the table, and the operator with the lowest precedence appears at the bottom. In an expression, the higher precedence operator is evaluated first.

Category | Description | Relevance |
---|---|---|

Suffix | ()[]->.++ -- | Left to Right |

Unary | + -!~++ --(type)*&sizeof | Right to Left |

Multiplication | */ % | Left to Right |

Addition | + - | Left to Right |

Shift | <<>> | Left to Right |

Relationship | <<=>>= | Left to Right |

Equality | ==!= | Left to Right |

Bitwise AND | & | Left to Right |

Bitwise XOR | ^ | Left to Right |

Bitwise OR | | | Left to Right |

Logical AND | && | Left to Right |

Logical OR | || | Left to Right |

Condition | ?: | Right to Left |

Assignment | =+=-=*=/= %=>>= <<= &= ^= |= | Right to Left |

Comma | , | Left to right |