diff --git a/example-go-channel/Makefile b/example-go-channel/Makefile index d74eec3..8406354 100644 --- a/example-go-channel/Makefile +++ b/example-go-channel/Makefile @@ -8,6 +8,10 @@ simple: GO111MODULE="off" go build -v -x -o ~/go/bin/simple-worker simple-worker.go simple-worker +main1: + GO111MODULE="off" go build -v -x -o ~/go/bin/main1 main.go + main1 + return-vals: GO111MODULE="off" go build -v -x -o ~/go/bin/return-vals return-vals.go return-vals diff --git a/example-go-channel/main.go b/example-go-channel/main.go index cdb25a3..2778133 100644 --- a/example-go-channel/main.go +++ b/example-go-channel/main.go @@ -12,21 +12,23 @@ 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) + fmt.Printf("START generateNumbers() sending %d to channel\n", idx) ch <- idx // res, err := (<-r)() - fmt.Printf("END sending %d to channel\n", idx) + fmt.Printf("END generateNumbers() sending %d to channel\n", idx) } wg.Wait() - fmt.Printf("END wg sending to channel\n") + fmt.Printf("END generateNumbers() wg sending to channel\n") } func printInt(idx int, ch <-chan int, wg *sync.WaitGroup) { defer wg.Done() + fmt.Printf("START printInt() for chan range\n") for num := range ch { fmt.Printf("%d: read %d from channel\n", idx, num) } + fmt.Printf("START printInt() for chan range\n") } func main() { diff --git a/example-go-channel/sendFunction.go b/example-go-channel/sendFunction.go index 269b129..f66916d 100644 --- a/example-go-channel/sendFunction.go +++ b/example-go-channel/sendFunction.go @@ -21,7 +21,6 @@ var afunc funcWait var wg sync.WaitGroup func generateNumbers(total int, ch chan<- *funcWait, af *funcWait) { - defer wg.Done() log.Println("generateNumbers() START total =", total) ch <- af @@ -33,7 +32,7 @@ func andlabsGoroutine(ch <-chan *funcWait, wg *sync.WaitGroup) { log.Println("andlabsGoroutine() START") for f := range ch { - log.Println("read f() from channel") + log.Println("andlabsGoroutine() read f() from channel") f.f() } log.Printf("andlabsGoroutine() END") @@ -58,8 +57,10 @@ func main() { afunc.val = idx * 20 generateNumbers(idx, functionChan, &afunc) } + wg.Done() close(functionChan) + // close(wg) log.Println("Waiting for goroutines to finish...") wg.Wait()