Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report