使用 Go 实现 Async/Await 模式

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"概述","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Golang 是一种并发编程语言。它具有强大的特性,如 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Goroutines","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Channels","attrs":{}}],"attrs":{}},{"type":"text","text":",可以很好地处理异步任务。另外,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"goroutines","attrs":{}}],"attrs":{}},{"type":"text","text":" 不是 OS 线程,这就是为什么您可以在不增加开销的情况下根据需要启动任意数量的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"goroutine","attrs":{}}],"attrs":{}},{"type":"text","text":" 的原因,它的堆栈大小初始化时仅 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"2KB","attrs":{}},{"type":"text","text":"。那么为什么要 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"async/await","attrs":{}}],"attrs":{}},{"type":"text","text":" 呢?","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Async/Await","attrs":{}}],"attrs":{}},{"type":"text","text":" 是一种很好的语言特点,它为异步编程提供了更简单的接口。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"项目链接:","attrs":{}},{"type":"link","attrs":{"href":"https://github.com/icyxp/AsyncGoDemo","title":""},"content":[{"type":"text","text":"https://github.com/icyxp/AsyncGoDemo","attrs":{}}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"它是如何工作的?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从 F# 开始,然后是 C#,到现在 Python 和 Javascript 中,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"async/await","attrs":{}}],"attrs":{}},{"type":"text","text":" 是一种非常流行的语言特点。它简化了异步方法的执行结构并且读起来像同步代码。对于开发人员来说更容易理解。让我们看看 c# 中的一个简单示例 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"async/await","attrs":{}}],"attrs":{}},{"type":"text","text":" 是如何工作的。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"csharp"},"content":[{"type":"text","text":"static async Task Main(string[] args)\n\n{\n\n Console.WriteLine(\"Let's start ...\");\n\n var done = DoneAsync();\n\n Console.WriteLine(\"Done is running ...\");\n\n Console.WriteLine(await done);\n\n}\n\nstatic async Task DoneAsync()\n\n{\n\n Console.WriteLine(\"Warming up ...\");\n\n await Task.Delay(3000);\n\n Console.WriteLine(\"Done ...\");\n\n return 1;\n\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当程序运行时,我们的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Main","attrs":{}}],"attrs":{}},{"type":"text","text":" 函数将被执行。我们有异步函数 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DoneAsync","attrs":{}}],"attrs":{}},{"type":"text","text":"。我们使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Delay","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法停止执行代码 3 秒钟。Delay 本身是一个异步函数,所以我们用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"await","attrs":{}}],"attrs":{}},{"type":"text","text":" 来调用它。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"await","attrs":{}}],"attrs":{}},{"type":"text","text":" 只阻塞异步函数内的代码执行 ","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Main","attrs":{}}],"attrs":{}},{"type":"text","text":" 函数中,我们不使用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"await","attrs":{}}],"attrs":{}},{"type":"text","text":" 来调用 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DoneAsync","attrs":{}}],"attrs":{}},{"type":"text","text":"。但 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DoneAsync","attrs":{}}],"attrs":{}},{"type":"text","text":" 开始执行后,只有当我们 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"await","attrs":{}}],"attrs":{}},{"type":"text","text":" 它的时候,我们才会得到结果。执行流程如下所示:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"Let's start ...\nWarming up ...\nDone is running ...\nDone ...\n1","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"对于异步执行,这看起来非常简单。让我们看看如何使用 Golang 的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Goroutines","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Channels","attrs":{}}],"attrs":{}},{"type":"text","text":" 来做到这一点。","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"go"},"content":[{"type":"text","text":"func DoneAsync() chan int {\n\n\tr := make(chan int)\n\n\tfmt.Println(\"Warming up ...\")\n\n\tgo func() {\n\n\t\ttime.Sleep(3 * time.Second)\n\n\t\tr
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章