Channel
🧠 1️⃣ Channel কী? (Basic Concept)
Channel হলো Go-এর built-in type, যা goroutines এর মধ্যে data পাঠাতে এবং গ্রহণ করতে ব্যবহার হয়।
Channel এর মাধ্যমে goroutines communicate safely করতে পারে, কোনো mutex বা extra locking ছাড়া।
মনে করো:
Channel = Pipe / Tube
Data travels safely from one goroutine to another
ch := make(chan int) // create a channel of type int
🔑 2️⃣ Why use channels?
Data passing safely → Race condition হবে না
Goroutine synchronization → Sender wait করবে worker ready না হলে
Communication between workers → Worker Pool, Pipeline, Fan-in/Fan-out
🧩 3️⃣ Channel Types
1️⃣ Unbuffered Channel
ch := make(chan int)
Sender wait করে যতক্ষণ receiver data নেয়
Receiver wait করে যতক্ষণ sender data দেয়
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
যখন আর data পাঠানো হবে না → close(channel)
Receiver জানবে কাজ শেষ হয়েছে
close(ch)
Receiver can still read remaining buffered data
🏗️ 6️⃣ Channel with Range Loop
for val := range ch {
fmt.Println(val)
}
Works only if channel is closed
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 Case | Example |
|---|---|
| Worker Pool | Jobs channel for tasks |
| Pipelines | Chain processing (data cleaning → transform → save) |
| Fan-in / Fan-out | Merge multiple input channels into one |
| Timeout / Cancellation | Combine with select + time.After / context |
| Event Handling | Channel as event bus |
| Rate Limiting | Only allow N jobs at a time |
💡 10️⃣ Key Notes
Channel = safe communication + synchronization
Buffered vs Unbuffered → sync vs async behavior
Close channels properly → avoid panic
Use
rangefor consuming channel dataCombine with WaitGroup → wait for all goroutines
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!
✅ কী হলো এখানে?
Sender:
ch <- 42-তে দাঁড়ালো → wait করছে যতক্ষণ receiver value নেবেReceiver: 2 সেকেন্ড পরে
val := <-chcall করলো → sender অবরোধ (blocked) উঠে গেল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
Unbuffered channel → Sender & Receiver must meet
Buffered channel → Sender wait না করেও data push করতে পারে until buffer full
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
| Feature | Unbuffered | Buffered |
|---|---|---|
| Blocking | Sender wait করবে যতক্ষণ receiver value নেবে | Sender wait করবে শুধু buffer full হলে |
| Sync | Synchronous | Asynchronous |
| Use case | Coordination | Queue / 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:
Sender তিনটি value পাঠাতে পারল একবারে, কারণ buffer capacity 3
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
প্রথম 2 value buffer এ stored হলো → sender blocked হলো না
3rd value পাঠানোর সময় sender blocked হলো → buffer full
✅ Key Points for Buffered Channels
Buffered channel size নির্ধারণ করা হলো capacity
Sender wait করবে buffer full না হলে → asynchronous
Receiver wait করবে buffer empty হলে
Useful for:
Job queues
Temporary storage
Rate limiting
Fan-out / Fan-in patterns