Skip to content

Instantly share code, notes, and snippets.

@mannion007
Created February 29, 2020 18:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mannion007/3c8899913974c1027ef6f13ec37b2b3f to your computer and use it in GitHub Desktop.
Save mannion007/3c8899913974c1027ef6f13ec37b2b3f to your computer and use it in GitHub Desktop.
Ninja level 10 challenge 7
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)
}
}
@Cijin
Copy link

Cijin commented Dec 3, 2021

Thank you for showing an alternative way of solving the problem.

@coalterd
Copy link

coalterd commented Dec 8, 2021

I like your solution

@alofeoluwafemi
Copy link

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