> ## 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.

# Go Channel

A channel in Go is a typed, thread-safe conduit used to send and receive values between concurrently executing goroutines. It acts as both a communication mechanism and a synchronization primitive, implemented internally as a heap-allocated queue structure (`hchan`) that manages goroutine scheduling states and memory access without requiring explicit mutexes from the developer.

## Initialization and Syntax

Channels are reference types. The zero value of an uninitialized channel is `nil`. To allocate memory and initialize the internal data structures, channels must be created using the built-in `make` function.

```go theme={"dark"}
// Declaration (nil channel)
var ch chan int

// Initialization
ch = make(chan int)

// Declaration and initialization combined
strChan := make(chan string)
```

## Capacity and Buffering

Channels are categorized by their capacity, which dictates their blocking behavior.

**1. Unbuffered Channels**
Created with a capacity of zero (the default). Operations are strictly synchronous. A send operation blocks the executing goroutine until another goroutine executes a receive operation on the same channel, and vice versa.

```go theme={"dark"}
unbuffered := make(chan float64) 
// or make(chan float64, 0)
```

**2. Buffered Channels**
Created with a capacity greater than zero. Operations are asynchronous up to the buffer limit. A send operation only blocks when the internal buffer is full. A receive operation only blocks when the internal buffer is empty.

```go theme={"dark"}
buffered := make(chan bool, 5) // Buffer capacity of 5
```

## Core Operations

The `<-` operator (the channel operator) specifies the direction of data flow.

**Sending**
Data flows into the channel.

```go theme={"dark"}
ch <- 42 // Blocks if unbuffered (and no receiver) or if buffer is full
```

**Receiving**
Data flows out of the channel.

```go theme={"dark"}
// Standard receive
val := <-ch 

// Comma-ok idiom (checks if channel is open or closed)
val, ok := <-ch 
```

*Note: If `ok` is `true`, the value was generated by a write. If `ok` is `false`, the channel is closed and `val` is the zero value of the channel's type.*

**Closing**
The built-in `close` function flags the channel to indicate that no more values will be sent. It is a state change, not a memory deallocation.

```go theme={"dark"}
close(ch)
```

## Directionality

By default, channels are bidirectional. However, they can be constrained to unidirectional types, typically within function signatures, to enforce compile-time type safety.

```go theme={"dark"}
// Bidirectional: can send and receive
func process(ch chan int) {}

// Send-only: compiler rejects receive operations (<-ch)
func writeOnly(ch chan<- int) {}

// Receive-only: compiler rejects send operations (ch <-) and close(ch)
func readOnly(ch <-chan int) {}
```

## State and Behavior Matrix

The behavior of channel operations strictly depends on the channel's current state (Nil, Open, or Closed).

| Operation   | Nil Channel         | Open Channel              | Closed Channel                            |
| :---------- | :------------------ | :------------------------ | :---------------------------------------- |
| **Send**    | Blocks indefinitely | Blocks if full / unready  | **Panics**                                |
| **Receive** | Blocks indefinitely | Blocks if empty / unready | Returns buffered values, then zero values |
| **Close**   | **Panics**          | Succeeds                  | **Panics**                                |

## Iteration

Channels integrate with Go's `range` loop. The loop continuously receives values from the channel until the channel is explicitly closed and its buffer is completely drained.

```go theme={"dark"}
for val := range ch {
    // Executes for every value received.
    // Loop terminates automatically when 'ch' is closed and empty.
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Go Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
