You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
660 B
Go
37 lines
660 B
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/tal-tech/go-zero/core/mr"
|
|
)
|
|
|
|
type User struct {
|
|
Uid int
|
|
Name string
|
|
}
|
|
|
|
func main() {
|
|
uids := []int{111, 222, 333}
|
|
res, err := mr.MapReduce(func(source chan<- interface{}) {
|
|
for _, uid := range uids {
|
|
source <- uid
|
|
}
|
|
}, func(item interface{}, writer mr.Writer, cancel func(error)) {
|
|
uid := item.(int)
|
|
user := &User{
|
|
Uid: uid,
|
|
Name: strconv.Itoa(uid),
|
|
}
|
|
writer.Write(user)
|
|
}, func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) {
|
|
// missing writer.Write(...), should not panic
|
|
})
|
|
if err != nil {
|
|
log.Print(err)
|
|
return
|
|
}
|
|
log.Print(len(res.([]*User)))
|
|
}
|