The example in this article describes how the go language generates primes. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package Main
Generate 2, 3, 4, ... Into the channel ' ch '.
Func Generate (ch chan<-int) {
For I: = 2;; i++ {
CH <-I//Send ' I ' to Channel ' ch '.
}
}
Copy value ' in ' from pipe to channel ' out ',
Remove the divisible number ' prime '.
Func Filter (in <-chan int, out chan<-int, prime int) {
for {
I: = <-in//Receive value ' in '.
If I%prime!= 0 {
Out <-I//incoming ' I ' to ' out '.
}
}
}
Func Main () {
CH: = make (chan int)//Create a newchannel.
Go Generate (CH)//Launch Generate goroutine.
For I: = 0; I < 10; i++ {
Prime: = <-ch
Print (prime, "\ n")
CH1: = make (chan int)
Go Filter (CH, ch1, Prime)
CH = ch1
}
}
I hope this article will help you with your go language program.