使用 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章