Compare commits
3 Commits
a2bd8f9c29
...
9b57621ecd
Author | SHA1 | Date |
---|---|---|
|
9b57621ecd | |
|
f4d755db16 | |
|
741286521a |
|
@ -8,6 +8,10 @@ simple:
|
||||||
GO111MODULE="off" go build -v -x -o ~/go/bin/simple-worker simple-worker.go
|
GO111MODULE="off" go build -v -x -o ~/go/bin/simple-worker simple-worker.go
|
||||||
simple-worker
|
simple-worker
|
||||||
|
|
||||||
|
main1:
|
||||||
|
GO111MODULE="off" go build -v -x -o ~/go/bin/main1 main.go
|
||||||
|
main1
|
||||||
|
|
||||||
return-vals:
|
return-vals:
|
||||||
GO111MODULE="off" go build -v -x -o ~/go/bin/return-vals return-vals.go
|
GO111MODULE="off" go build -v -x -o ~/go/bin/return-vals return-vals.go
|
||||||
return-vals
|
return-vals
|
||||||
|
|
|
@ -12,21 +12,23 @@ func generateNumbers(total int, ch chan<- int, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
for idx := 1; idx <= total; idx++ {
|
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
|
ch <- idx
|
||||||
// res, err := (<-r)()
|
// res, err := (<-r)()
|
||||||
fmt.Printf("END sending %d to channel\n", idx)
|
fmt.Printf("END generateNumbers() sending %d to channel\n", idx)
|
||||||
}
|
}
|
||||||
wg.Wait()
|
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) {
|
func printInt(idx int, ch <-chan int, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
fmt.Printf("START printInt() for chan range\n")
|
||||||
for num := range ch {
|
for num := range ch {
|
||||||
fmt.Printf("%d: read %d from channel\n", idx, num)
|
fmt.Printf("%d: read %d from channel\n", idx, num)
|
||||||
}
|
}
|
||||||
|
fmt.Printf("START printInt() for chan range\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
|
@ -4,55 +4,65 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var functionChan chan func (*sync.WaitGroup)
|
|
||||||
|
|
||||||
type funcWait struct {
|
type funcWait struct {
|
||||||
f func()
|
f func()
|
||||||
wgF sync.WaitGroup
|
wgF sync.WaitGroup
|
||||||
|
val int
|
||||||
err error
|
err error
|
||||||
res int
|
res int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var functionChan chan *funcWait
|
||||||
|
var afunc funcWait
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
func generateNumbers(total int, ch chan<- func (), wg *sync.WaitGroup) {
|
func generateNumbers(total int, ch chan<- *funcWait, af *funcWait) {
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
fmt.Printf("sending %d to channel\n", total)
|
log.Println("generateNumbers() START total =", total)
|
||||||
ch <- func() {
|
ch <- af
|
||||||
fmt.Printf("inside f() sending %d to channel\n", total)
|
log.Println("generateNumbers() END total =", total)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func andlabsGoroutine(ch <-chan func(), wg *sync.WaitGroup) {
|
func andlabsGoroutine(ch <-chan *funcWait, wg *sync.WaitGroup) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
|
log.Println("andlabsGoroutine() START")
|
||||||
for f := range ch {
|
for f := range ch {
|
||||||
fmt.Printf("read f() from channel\n")
|
log.Println("andlabsGoroutine() read f() from channel")
|
||||||
f()
|
f.f()
|
||||||
}
|
}
|
||||||
|
log.Printf("andlabsGoroutine() END")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
functionChan = make(chan func(*sync.WaitGroup))
|
functionChan = make(chan *funcWait)
|
||||||
|
|
||||||
r := make(chan func() (int, error))
|
// syntax to get the return values from a channel
|
||||||
res, err = (<-r)()
|
// r := make(chan func() (int, error))
|
||||||
|
// afunc.res, afunc.err = (<-r)()
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go andlabsGoroutine(functionChan, &wg)
|
go andlabsGoroutine(functionChan, &wg)
|
||||||
|
|
||||||
for idx := 1; idx <= total; idx++ {
|
// afunc.wgF = wg
|
||||||
generateNumbers(idx, functionChan, &wg)
|
afunc.f = func() {
|
||||||
|
log.Println("\tGOT INSIDE: running f() from inside the structure & inside the wait group goroutine", afunc.val)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(functionChan)
|
for idx := 1; idx <= 10; idx++ {
|
||||||
|
afunc.val = idx * 20
|
||||||
|
generateNumbers(idx, functionChan, &afunc)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
|
||||||
fmt.Println("Waiting for goroutines to finish...")
|
close(functionChan)
|
||||||
|
// close(wg)
|
||||||
|
|
||||||
|
log.Println("Waiting for goroutines to finish...")
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("Done!")
|
log.Println("Done!")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue