Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
x |= y
This is a syntactic shorthand equivalent to:
x = x | y

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

package main

import "fmt"

func main() {
    var a uint8 = 10 // Binary: 00001010
    var b uint8 = 6  // Binary: 00000110

    a |= b           // Evaluates: 00001010 | 00000110

    fmt.Printf("%08b\n", a) // Output: 00001110 (Decimal: 14)
}

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.
var x int32 = 4
var y int64 = 8

// x |= y // Compile error: invalid operation: mismatched types int32 and int64
x |= int32(y) // Valid
Master Go with Deep Grasping Methodology!Learn More