irregular.go 497 B

12345678910111213141516171819202122232425262728
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/tal-tech/go-zero/core/mr"
  6. )
  7. func main() {
  8. mr.MapReduceVoid(func(source chan<- interface{}) {
  9. for i := 0; i < 10; i++ {
  10. source <- i
  11. }
  12. }, func(item interface{}, writer mr.Writer, cancel func(error)) {
  13. i := item.(int)
  14. if i == 0 {
  15. time.Sleep(10 * time.Second)
  16. } else {
  17. time.Sleep(5 * time.Second)
  18. }
  19. writer.Write(i)
  20. }, func(pipe <-chan interface{}, cancel func(error)) {
  21. for i := range pipe {
  22. fmt.Println(i)
  23. }
  24. })
  25. }