Header Ads

Header ADS

Channel

 



🧠 1️⃣ Channel কী? (Basic Concept)

Channel হলো Go-এর built-in type, যা goroutines এর মধ্যে data পাঠাতে এবং গ্রহণ করতে ব্যবহার হয়।
Channel এর মাধ্যমে goroutines communicate safely করতে পারে, কোনো mutex বা extra locking ছাড়া।

মনে করো:

  1. Channel = Pipe / Tube

  2. Data travels safely from one goroutine to another

ch := make(chan int) // create a channel of type int

🔑 2️⃣ Why use channels?

  1. Data passing safely → Race condition হবে না

  2. Goroutine synchronization → Sender wait করবে worker ready না হলে

  3. Communication between workers → Worker Pool, Pipeline, Fan-in/Fan-out


🧩 3️⃣ Channel Types

1️⃣ Unbuffered Channel

ch := make(chan int)
  1. Sender wait করে যতক্ষণ receiver data নেয়

  2. Receiver wait করে যতক্ষণ sender data দেয়

  3. Synchronous communication

package main

import "fmt"

func main() {
    ch := make(chan int)

    go func() {
        ch <- 42 // sender blocks until main receives
    }()

    fmt.Println(<-ch) // receiver blocks until data sent
}

2️⃣ Buffered Channel

ch := make(chan int, 3) // buffer size 3
  • Sender can send up to buffer size data without waiting

  • Receiver reads when ready

  • Asynchronous communication

ch <- 1
ch <- 2
ch <- 3 // all sent without waiting

🔄 4️⃣ Send & Receive Syntax

ch <- value   // send value to channel
value := <-ch // receive value from channel

⛔ 5️⃣ Close a channel

  1. যখন আর data পাঠানো হবে না → close(channel)

  2. Receiver জানবে কাজ শেষ হয়েছে

close(ch)
  1. Receiver can still read remaining buffered data


🏗️ 6️⃣ Channel with Range Loop

for val := range ch {
    fmt.Println(val)
}
  1. Works only if channel is closed

  2. Reads until channel empty


🧠 7️⃣ Select Statement (Advanced Communication)

Select allows a goroutine to wait on multiple channels

select {
case msg1 := <-ch1:
    fmt.Println("Received", msg1)
case ch2 <- 42:
    fmt.Println("Sent 42")
default:
    fmt.Println("No channel ready")
}
  • Useful for timeouts, fan-in, fan-out, multi-channel operations


🧩 8️⃣ Real-World Use Cases

Use CaseExample
Worker PoolJobs channel for tasks
PipelinesChain processing (data cleaning → transform → save)
Fan-in / Fan-outMerge multiple input channels into one
Timeout / CancellationCombine with select + time.After / context
Event HandlingChannel as event bus
Rate LimitingOnly allow N jobs at a time


💡 10️⃣ Key Notes

  1. Channel = safe communication + synchronization

  2. Buffered vs Unbuffered → sync vs async behavior

  3. Close channels properly → avoid panic

  4. Use range for consuming channel data

  5. Combine with WaitGroup → wait for all goroutines

  6. Select → handle multiple channels, timeouts



 

 Example: Synchronous Channel (Unbuffered)

package main

import (
    "fmt"
    "time"
)

func sender(ch chan int) {
    fmt.Println("Sender: Sending value 42...")
    ch <- 42 // Sender এখানে block হবে যতক্ষণ receiver value নেবে
    fmt.Println("Sender: Value sent!")
}

func receiver(ch chan int) {
    time.Sleep(2 * time.Second) // Simulate delay
    val := <-ch // Receiver block হবে যতক্ষণ sender value পাঠাবে
    fmt.Println("Receiver: Received", val)
}

func main() {
    ch := make(chan int) // Unbuffered channel

    go sender(ch)
    go receiver(ch)

    // wait for goroutines to finish
    time.Sleep(3 * time.Second)
}

🔥 Output Explanation

Sender: Sending value 42...
Receiver: Received 42
Sender: Value sent!

✅ কী হলো এখানে?

  1. Sender: ch <- 42-তে দাঁড়ালো → wait করছে যতক্ষণ receiver value নেবে

  2. Receiver: 2 সেকেন্ড পরে val := <-ch call করলো → sender অবরোধ (blocked) উঠে গেল

  3. Sender: value পাঠানোর পর print হলো "Sender: Value sent!"


🧠 Mental Model

  • Sender = Wants to hand over data

  • Receiver = Needs to take the data

  • Unbuffered channel = Only one can proceed when both are ready

  • এটাকেই বলে synchronous communication

Analogy:

  • Sender = Someone passing a note

  • Receiver = Someone taking the note

  • যদি receiver ready না থাকে → sender wait করবে

  • যদি sender ready না থাকে → receiver wait করবে



Key Takeaways

  1. Unbuffered channel → Sender & Receiver must meet

  2. Buffered channel → Sender wait না করেও data push করতে পারে until buffer full

  3. Synchronous communication → perfect for coordination, safe communication

     

     

🧠 Buffered Channel কী?

Buffered Channel হলো এমন একটি channel যার মধ্যে একটি fixed capacity buffer থাকে, তাই sender কিছু value পাঠাতে পারে receiver ready না থাকলেও
অর্থাৎ এটি asynchronous communication দেয়।


🔑 Main Differences: Buffered vs Unbuffered

FeatureUnbufferedBuffered
Blocking  Sender wait করবে যতক্ষণ receiver value নেবেSender wait করবে শুধু buffer full হলে
Sync  SynchronousAsynchronous
Use case  CoordinationQueue / temporary storage

🏗️ Buffered Channel Syntax

ch := make(chan int, 3) // buffer size 3
  • অর্থ: channel buffer-এ একসাথে 3টি value রাখতে পারবে

  • Sender wait করবে না যতক্ষণ buffer full না হয়


🧩 Example: Simple Buffered Channel

package main

import "fmt"

func main() {
    ch := make(chan int, 3) // buffered channel with size 3

    // Sending values
    fmt.Println("Sending 1")
    ch <- 1
    fmt.Println("Sending 2")
    ch <- 2
    fmt.Println("Sending 3")
    ch <- 3

    fmt.Println("All 3 values sent without blocking")

    // Receiving values
    fmt.Println("Receiving:", <-ch)
    fmt.Println("Receiving:", <-ch)
    fmt.Println("Receiving:", <-ch)
}

🔥 Output

Sending 1
Sending 2
Sending 3
All 3 values sent without blocking
Receiving: 1
Receiving: 2
Receiving: 3

Observation:

  1. Sender তিনটি value পাঠাতে পারল একবারে, কারণ buffer capacity 3

  2. Receiver পরে ধাপে ধাপে value পড়ল



🏗️ Example with Goroutine

package main

import (
    "fmt"
    "time"
)

func main() {
    ch := make(chan int, 2) // buffer size 2

    go func() {
        for i := 1; i <= 4; i++ {
            fmt.Println("Sending", i)
            ch <- i
            fmt.Println("Sent", i)
        }
    }()

    time.Sleep(2 * time.Second)

    go func() {
        for i := 1; i <= 4; i++ {
            fmt.Println("Received", <-ch)
        }
    }()

    time.Sleep(5 * time.Second)
}

🔑 Output (Sample)

Sending 1
Sent 1
Sending 2
Sent 2
Sending 3  <- here sender blocks because buffer size = 2
Sent 3
Sending 4
Sent 4
Received 1
Received 2
Received 3
Received 4
  1. প্রথম 2 value buffer এ stored হলো → sender blocked হলো না

  2. 3rd value পাঠানোর সময় sender blocked হলো → buffer full


✅ Key Points for Buffered Channels

  1. Buffered channel size নির্ধারণ করা হলো capacity

  2. Sender wait করবে buffer full না হলে → asynchronous

  3. Receiver wait করবে buffer empty হলে

  4. Useful for:

    1. Job queues

    2. Temporary storage

    3. Rate limiting

    4. Fan-out / Fan-in patterns





Powered by Blogger.