From e66d76ddc9a81fecab9db607e8f300a73029e8a2 Mon Sep 17 00:00:00 2001 From: lbenedar Date: Sat, 21 Feb 2026 14:41:54 +0300 Subject: [PATCH] add changes to ch12_1 --- .gitignore | 1 + ch12/1/ch12_1.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 .gitignore create mode 100644 ch12/1/ch12_1.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..adb36c8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/ch12/1/ch12_1.go b/ch12/1/ch12_1.go new file mode 100644 index 0000000..d79fb10 --- /dev/null +++ b/ch12/1/ch12_1.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "math/rand" +) + +func PrintNumbers() { + const IN_CHAN_NUM = 2 + numCh := make(chan int, 20) + completeCh := make(chan int) + defer close(numCh) + defer close(completeCh) + for i := 0; i < IN_CHAN_NUM; i++ { + go func() { + for j := 0; j < 10; j++ { + numCh <- (rand.Int() % 100) + } + }() + } + go func() { + i := 0 + for num := range numCh { + fmt.Println(num) + i++ + if i == 20 { + break + } + } + completeCh <- 1 + }() + select { + case <-completeCh: + } +} + +func main() { + PrintNumbers() +}