Sample here.
This program give a amount of Zero in number from 0 to 1000.
package main
import(
"fmt"
"strings"
)
func generate(ch chan int) {
i := 0
for {
fmt.Printf("%d Send\n",i )
ch <- i
i ++
}
}
func main() {
ch := make(chan int)
myCnt := 0
go generate(ch)
for {
i := <-ch
fmt.Printf("%d Recieve\n",i )
if i <= 1000 {
myCnt += strings.Count(fmt.Sprintf("%d",i ),"0")
} else {
fmt.Printf("Completely \n")
break
}
}
fmt.Printf("The amount of Zero is %d ",myCnt)
}
On WindowsPort, Recieve channel ( line 24) has fault when Send channel has range like “for 1:=0;i<1000;i++ {}” is closed.
So we must have eternaly “for{}” for Send channel and control to colse channel in Recieve channel logic.