This repository demonstrates the concept of closing channels in Go. It shows how to close a channel after sending all values and how to use a range
loop to receive values until the channel is closed.
- This example covers basic channel closing operations in Go.
- It explains how to close a channel after sending all values and how to handle channel closure during value reception using a range loop.
package main
import "fmt"
func main() {
// Basic Channel Closing
// In this example, the channel is closed using close(ch) after all values are sent.
// The range loop is used to receive values until the channel is closed.
// Once the channel is closed, the loop automatically exits.
ch := make(chan int)
go func() {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch) // Close the channel after sending all values
}()
// Receiving values until the channel is closed
for value := range ch {
fmt.Println("Received:", value)
}
fmt.Println("Channel closed, no more data.")
}
- Make sure you have Go installed. If not, you can download it from here.
- Clone this repository:
git clone https://github.com/Rapter1990/go_sample_examples.git
- Navigate to the
018_closing_channel/01_basic_channel_closing
directory:
cd go_sample_examples/018_closing_channel/01_basic_channel_closing
- Run the Go program:
go run 01_basic_channel_closing.go
When you run the program, you should see the following output:
Received: 0
Received: 1
Received: 2
Received: 3
Received: 4
Channel closed, no more data.