Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use go language to simply simulate the generator of Python

Shulou Source: shulou.com Published: 2022-06-03 11:54:24 09月10日 Update

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

Tags: Code blocking output that is at the same time data generator language generation up and down context content effect version analysis switching different one-time no Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Redmi Shulou Information vpn