How to use go language to simply simulate the generator of Python
In this issue, the editor will bring you a generator about how to use GE language to simply simulate Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Def demo_input_and_output (): input = yield 'what is the input?' Yield 'input is:% s'% inputgen = demo_input_and_output () print (gen.next ()) print (gen.send (42))
This code demonstrates the functionality of python generator. You can see that yield does two operations at the same time, one is to send the data "waht is the input", while the operation is to receive the data input. And the operation that receives data is a blocking operation, and if there is no external call to next () (that is, passing None in), or a call to send (42) (that is, passing the value 42 in), then the blocking operation will wait forever.
In other words, the generator of python comes with a channel for external communication, which is used to send and receive messages. That's what it looks like when you use go to simulate the generator of python.
The copy code is as follows:
Package main
Import "fmt"
Func demoInputAndOutput (channel chan string) {
Channel