From 15efa4258e197085a9cd889244794bc8ac07f592 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 13 Nov 2022 14:19:18 -0600 Subject: [PATCH] more examples --- example-go-channel/5threads.go | 45 +++++++++++++++++++++++++++++ example-go-channel/Makefile | 7 +++-- example-go-channel/main.go | 5 +++- example-go-channel/simple-worker.go | 33 +++++++++++++++++++++ 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 example-go-channel/5threads.go create mode 100644 example-go-channel/simple-worker.go diff --git a/example-go-channel/5threads.go b/example-go-channel/5threads.go new file mode 100644 index 0000000..35d8e66 --- /dev/null +++ b/example-go-channel/5threads.go @@ -0,0 +1,45 @@ +// https://www.digitalocean.com/community/tutorials/how-to-run-multiple-functions-concurrently-in-go +// who came up with the idea of making community tutorials. that was a good idea! + +package main + +import ( + "fmt" + "sync" +) + +func generateNumbers(total int, ch chan<- int, wg *sync.WaitGroup) { + defer wg.Done() + + for idx := 1; idx <= total; idx++ { + fmt.Printf("START sending %d to channel\n", idx) + ch <- idx + } +} + +func printInt(idx int, ch <-chan int, wg *sync.WaitGroup) { + defer wg.Done() + + for num := range ch { + fmt.Printf("%d: read %d from channel\n", idx, num) + } +} + +func main() { + var wg sync.WaitGroup + numberChan := make(chan int) + + for idx := 1; idx <= 5; idx++ { + fmt.Printf("START printInt %d\n", idx) + wg.Add(1) + go printInt(idx, numberChan, &wg) + } + + generateNumbers(20, numberChan, &wg) + + close(numberChan) + + fmt.Println("Waiting for goroutines to finish...") + wg.Wait() + fmt.Println("Done!") +} diff --git a/example-go-channel/Makefile b/example-go-channel/Makefile index bdb7f0e..eea6cea 100644 --- a/example-go-channel/Makefile +++ b/example-go-channel/Makefile @@ -1,3 +1,6 @@ build: - GO111MODULE="off" go build -v -x - ./go-channels + GO111MODULE="off" go build -v -x -o ~/go/bin/5threads 5threads.go + 5threads + +simple: + GO111MODULE="off" go build -v -x -o ~/go/bin/simple-worker simple-worker.go diff --git a/example-go-channel/main.go b/example-go-channel/main.go index e19b132..a986987 100644 --- a/example-go-channel/main.go +++ b/example-go-channel/main.go @@ -12,9 +12,12 @@ func generateNumbers(total int, ch chan<- int, wg *sync.WaitGroup) { defer wg.Done() for idx := 1; idx <= total; idx++ { - fmt.Printf("sending %d to channel\n", idx) + fmt.Printf("START sending %d to channel\n", idx) ch <- idx + fmt.Printf("END sending %d to channel\n", idx) } + wg.Wait() + fmt.Printf("END wg sending to channel\n") } func printInt(idx int, ch <-chan int, wg *sync.WaitGroup) { diff --git a/example-go-channel/simple-worker.go b/example-go-channel/simple-worker.go new file mode 100644 index 0000000..1b46818 --- /dev/null +++ b/example-go-channel/simple-worker.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "sync" + "time" +) + +func worker(id int) { + fmt.Printf("Worker %d starting\n", id) + + time.Sleep(time.Second) + fmt.Printf("Worker %d done\n", id) +} + +func main() { + + var wg sync.WaitGroup + + for i := 1; i <= 5; i++ { + wg.Add(1) + + i := i + + go func() { + defer wg.Done() + worker(i) + }() + } + + wg.Wait() + +}