Skip to main content

channel

Channels provide a safe way for concurrent goroutines to communicate with each other in Soul. They allow you to send and receive values between different parts of your concurrent program, preventing race conditions and ensuring thread-safe data sharing.

Creating Channels

Channels are created using the Concurrency.createChannel() function:

Channel Send Operations

Send values to channels using the <- operator or the send() method:

Channel Receive Operations

Receive values from channels using the <- operator or the receive() method:

Buffered vs Unbuffered Channels

Unbuffered Channels (Synchronous)

Buffered Channels (Asynchronous)

Select Statements

Select statements allow you to work with multiple channel operations:

Channel Patterns

Producer-Consumer Pattern

Fan-Out Pattern

Worker Pool Pattern

Channel Methods

Channels support several useful methods:

Channel with Select and Timeout

Best Practices

  1. Close channels when done: Always close channels when no more data will be sent
  2. Use buffered channels for async: When you don’t need synchronous communication
  3. Use select for multiple channels: Don’t block on single channel operations
  4. Handle channel closure: Check if channels are closed before sending
Channels are fundamental to Soul’s concurrency model, providing a safe and efficient way to coordinate between concurrent operations.