代码
package main
import (
"fmt"
"time"
)
type Dto struct {
Name string
Age int64
}
func Deal(in <-chan *Dto, out chan<- *Dto) {
in1 := <-in
newDto := &Dto{
Name: in1.Name,
Age: in1.Age,
}
if in1.Name == "xiao1" {
newDto.Age += 100
}
out <- in1
}
func main() {
in, out := make(chan *Dto), make(chan *Dto)
contents := []*Dto{
&Dto{Name: "xiao", Age: 18},
&Dto{Name: "xiao1", Age: 19},
&Dto{Name: "xiao2", Age: 20},
&Dto{Name: "xiao3", Age: 21},
&Dto{Name: "xiao4", Age: 22},
}
for _, v := range contents {
go func() {
in <- v
}()
go Deal(in, out)
}
count := len(contents)
for index := 0; index < count; index++ {
fmt.Println(<-out)
}
close(in)
fmt.Println("end")
time.Sleep(100 * time.Minute)
}
实际输出
&{xiao4 22}
&{xiao4 22}
&{xiao4 22}
&{xiao4 22}
&{xiao4 22}
预期产量
&{xiao 18}
&{xiao1 119}
&{xiao2 20}
&{xiao3 21}
&{xiao4 22}