Created
February 29, 2020 18:40
-
-
Save mannion007/3c8899913974c1027ef6f13ec37b2b3f to your computer and use it in GitHub Desktop.
Ninja level 10 challenge 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
c := make(chan int) | |
go func() { | |
for i := 0; i < 10; i++ { | |
wg.Add(1) | |
go func(m int) { | |
for i := 0; i < 10; i++ { | |
c <- i*10 + m | |
} | |
wg.Done() | |
}(i) | |
} | |
wg.Wait() | |
close(c) | |
}() | |
for v := range c { | |
fmt.Println(v) | |
} | |
} |
I like your solution
I also tried this:
import (
"fmt"
"sync"
)
func main() {
c := make(chan int)
var wg sync.WaitGroup
steps := 10
var cycle int
wg.Add(steps)
for j := 0; j < steps; j++ {
cycle += steps
go func() {
for i := 0; i < steps; i++ {
c <- i
}
}()
wg.Done()
}
wg.Wait()
k := 0
for v := range c {
fmt.Println(k, v, cycle)
k++
if k == cycle {
break
}
}
fmt.Println("about to exit")
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for showing an alternative way of solving the problem.