main.go 660 B

123456789101112131415161718192021222324252627282930313233343536
  1. package main
  2. import (
  3. "log"
  4. "strconv"
  5. "github.com/tal-tech/go-zero/core/mr"
  6. )
  7. type User struct {
  8. Uid int
  9. Name string
  10. }
  11. func main() {
  12. uids := []int{111, 222, 333}
  13. res, err := mr.MapReduce(func(source chan<- interface{}) {
  14. for _, uid := range uids {
  15. source <- uid
  16. }
  17. }, func(item interface{}, writer mr.Writer, cancel func(error)) {
  18. uid := item.(int)
  19. user := &User{
  20. Uid: uid,
  21. Name: strconv.Itoa(uid),
  22. }
  23. writer.Write(user)
  24. }, func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) {
  25. // missing writer.Write(...), should not panic
  26. })
  27. if err != nil {
  28. log.Print(err)
  29. return
  30. }
  31. log.Print(len(res.([]*User)))
  32. }