Skip to main content
The |= operator is the bitwise OR assignment operator in Go. It performs a bitwise OR operation between the left and right integer operands and assigns the resulting value directly to the left operand.
This is a syntactic shorthand equivalent to:

Mechanics

The operator evaluates the binary representation of both operands. For each corresponding bit position, it applies the logical OR operation: if at least one of the compared bits is 1, the resulting bit is set to 1. If both bits are 0, the resulting bit remains 0. Bitwise OR Truth Table: | Bit x | Bit y | Result (x | y) | | :---: | :---: | :---: | | 0 | 0 | 0 | | 0 | 1 | 1 | | 1 | 0 | 1 | | 1 | 1 | 1 |

Code Visualization

Type Constraints

  1. Integer Types Only: The |= operator can only be applied to integer types (e.g., int, int32, uint8, byte, rune). Attempting to use it with floating-point numbers (float32, float64), complex numbers, or non-numeric types will result in a compile-time error.
  2. Type Matching: The right-hand operand must be assignable to the type of the left-hand operand. If they are of different explicitly declared integer types, you must perform a type conversion before applying the operator.
Tired of Poor Go Skills? Fix That With Deep Grasping!Learn More