-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_generic.go
63 lines (49 loc) · 950 Bytes
/
example_generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
. "iter_generic"
"strings"
)
type Person struct {
Name string
Age int
}
func input() Chan {
persons_array := []Person{
Person{"Joe", 10},
Person{"Jane", 40},
Person{"Jim", 50},
Person{"John", 60},
}
persons_chan := make(Chan)
go func() {
defer close(persons_chan)
for _, w := range persons_array {
persons_chan <- w
}
}()
return persons_chan
}
func main_generic() {
input_channel := input()
for item := range input_channel {
fmt.Printf("%v, %v\n", item.(Person).Name, item.(Person).Age)
}
fmt.Println("- - -")
input_channel = input()
cb := func(c1, c2 Chan) Chan {
for person := range c1 {
if person.(Person).Age > 18 {
p := Person{
strings.ToUpper(person.(Person).Name),
person.(Person).Age}
c2 <- p
}
}
return c2
}
electors := Map(input_channel, cb)
for person := range electors {
fmt.Printf("Elector --> %v\n", person.(Person))
}
}