Build a Simple Channel Data Pipeline

GolangGolangBeginner
Practice Now

Introduction

In this challenge, you'll create a Go program that demonstrates the power of channels by building a simple data processing pipeline that transfers integers between goroutines. You'll need to create a buffered channel, write the numbers 1-5 into the channel, and then read and print each number from the channel in the order they were written.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("Golang")) -.-> go/ConcurrencyGroup(["Concurrency"]) go/ConcurrencyGroup -.-> go/goroutines("Goroutines") go/ConcurrencyGroup -.-> go/channels("Channels") go/ConcurrencyGroup -.-> go/select("Select") subgraph Lab Skills go/goroutines -.-> lab-437199{{"Build a Simple Channel Data Pipeline"}} go/channels -.-> lab-437199{{"Build a Simple Channel Data Pipeline"}} go/select -.-> lab-437199{{"Build a Simple Channel Data Pipeline"}} end

Build a Simple Channel Data Pipeline

In this challenge, you'll create a Go program that demonstrates the power of channels by building a simple data processing pipeline that transfers integers between goroutines.

Tasks

  • Create a buffered channel of integers with a capacity of 5
  • Write the numbers 1, 2, 3, 4, and 5 into the channel
  • Read and print each number from the channel in the order they were written

Requirements

  • Use the file ~/project/data_pipeline.go
  • Create a buffered channel using make(chan int, 5)
  • Use the channel send operator <- to write numbers to the channel
  • Use the channel receive operator <- to read numbers from the channel
  • Print each number using fmt.Println()
  • Ensure all numbers are written and read completely

Examples

Run the program:

go run data_pipeline.go

Expected output:

1
2
3
4
5

Hints

  • Remember to use ch <- value to send values to the channel
  • Use value := <-ch to receive values from the channel
  • Buffered channels allow sending multiple values before blocking
  • Close the channel after writing all values if needed
โœจ Check Solution and Practice

Summary

In summary, this challenge requires you to create a Go program that uses channels to build a simple data processing pipeline. You'll need to create a buffered channel, write the numbers 1-5 into the channel, and then read and print each number from the channel in the order they were written.