Package Main
Import (
"FMT"
)
Func files (fs []string) <-chan string{
c: = Make (Chan string, 1000)//with 1000 buffered channel,1000 without blocking
Go func () {
For _, F: = Range fs{
C <-F
}
Close (c)//Slice All files are sent, closed
}()
Return C
}
Func filtersize (in <-chan string) <-chan string{
c: = Make (Chan string, 1000)//with 1000 buffered channel,1000 without blocking
Go func () {
For I: = Range in{
Fmt. Println ("Filter size got:", i)
if i = = "123.txt" | | i = = "456.txt" {
C <-I
}
}
Close (c)
}()
Return C
}
Func Main () {
Fmt. Println ("File filter Service")
FS: = []string{"123.txt", "456.txt", "789.txt", "1222.txt"}
c: = filtersize (Files (FS))
For I: = Range c{//block main gorouting, loop through the data in the channel until the channel is closed
Fmt. Println ("Main got:", i)
}
}
Output:
File Filter Service
Filter size Func got:123.txt
Filter size Func got:456.txt
Filter size Func got:789.txt
Filter size Func got:1222.txt
Main Got:123.txt
Main Got:456.txt