亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

首頁 后端開發(fā) Golang 通過直觀的視覺效果了解 Golang 中的 Goroutines 和 Channel

通過直觀的視覺效果了解 Golang 中的 Goroutines 和 Channel

Dec 30, 2024 pm 04:18 PM

?? 這個系列如何進行?

1。運行每個示例:不要只閱讀代碼。輸入它,運行它,然后觀察其行為。
2。實驗和打破常規(guī): 刪除睡眠并看看會發(fā)生什么,更改通道緩沖區(qū)大小,修改 goroutine 計數(shù)。
打破東西會教你它們是如何工作的
3。關于行為的原因: 在運行修改后的代碼之前,嘗試預測結果。當您看到意外行為時,請停下來思考原因。挑戰(zhàn)解釋。
4。建立心理模型:每個可視化代表一個概念。嘗試為修改后的代碼繪制自己的圖表。

Understanding Goroutines and Channels in Golang with Intuitive Visuals

這是“掌握 Go 并發(fā)”系列的 第 1 部分,我們將介紹:

  • goroutine 的工作原理及其生命周期
  • goroutines 之間的通道通信
  • 緩沖通道及其用例
  • 實際示例和可視化

我們將從基礎知識開始,逐步發(fā)展如何有效使用它們的直覺。

這會有點長,相當長,所以做好準備。

Understanding Goroutines and Channels in Golang with Intuitive Visuals

我們將親力親為完成整個過程。

Goroutine 的基礎

讓我們從一個下載多個文件的簡單程序開始。

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now()

    downloadFile("file1.txt")
    downloadFile("file2.txt")
    downloadFile("file3.txt")

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

Understanding Goroutines and Channels in Golang with Intuitive Visuals

該程序總共需要 6 秒,因為每個 2 秒的下載必須在下一個開始之前完成。讓我們想象一下:

Understanding Goroutines and Channels in Golang with Intuitive Visuals

我們可以縮短這個時間,讓我們修改我們的程序以使用go例程

注意:函數(shù)調用前使用 go 關鍵字

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    // Launch downloads concurrently
    go downloadFile("file1.txt")
    go downloadFile("file2.txt")
    go downloadFile("file3.txt")

    fmt.Println("All downloads completed!")
}

等等什么?沒有打印任何內容?為什么?

Understanding Goroutines and Channels in Golang with Intuitive Visuals

讓我們想象一下這一點,以了解可能發(fā)生的情況。

Understanding Goroutines and Channels in Golang with Intuitive Visuals

從上面的可視化中,我們了解到 main 函數(shù)在 goroutine 完成之前就存在了。一項觀察結果是,所有 goroutine 的生命周期都依賴于 main 函數(shù)。

注意:main函數(shù)本身就是一個goroutine;)

為了解決這個問題,我們需要一種方法讓主 goroutine 等待其他 goroutine 完成。有幾種方法可以做到這一點:

  1. 等待幾秒鐘(黑客方式)
  2. 使用 WaitGroup(正確的方法,下一步)
  3. 使用頻道(我們將在下面介紹)

讓我們等待幾秒鐘讓 go 例程完成。

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now()

    downloadFile("file1.txt")
    downloadFile("file2.txt")
    downloadFile("file3.txt")

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

問題是,我們可能不知道 goroutine 可能需要多長時間。在這種情況下,我們每個人都有固定的時間,但在實際場景中,我們知道下載時間會有所不同。

來了sync.WaitGroup

Go中的sync.WaitGroup是一種并發(fā)控制機制,用于等待一組goroutines執(zhí)行完成。

在這里讓我們看看它的實際效果并可視化:

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    // Launch downloads concurrently
    go downloadFile("file1.txt")
    go downloadFile("file2.txt")
    go downloadFile("file3.txt")

    fmt.Println("All downloads completed!")
}

Understanding Goroutines and Channels in Golang with Intuitive Visuals

讓我們可視化這一點并了解sync.WaitGroup 的工作原理:

Understanding Goroutines and Channels in Golang with Intuitive Visuals

計數(shù)器機制:

  • WaitGroup 維護一個內部計數(shù)器
  • wg.Add(n) 將計數(shù)器增加 n
  • wg.Done() 將計數(shù)器減 1
  • wg.Wait() 阻塞,直到計數(shù)器達到 0

同步流程:

  • 主 Goroutine 在啟動 Goroutines 之前調用 Add(3)
  • 每個 goroutine 完成時都會調用 Done()
  • 主協(xié)程在 Wait() 處被阻塞,直到計數(shù)器達到 0
  • 當計數(shù)器達到 0 時,程序繼續(xù)并干凈地退出

要避免的常見陷阱
package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now() // Record start time

    go downloadFile("file1.txt")
    go downloadFile("file2.txt")
    go downloadFile("file3.txt")

    // Wait for goroutines to finish
    time.Sleep(3 * time.Second)

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

渠道

這樣我們就很好地理解了 goroutine 是如何工作的。不,兩個 Go 例程如何通信?這就是頻道發(fā)揮作用的地方。

Go 中的

Channels 是一個強大的并發(fā)原語,用于 goroutine 之間的通信。它們?yōu)?goroutine 提供了一種安全共享數(shù)據(jù)的方法。

將通道視為管道:一個 goroutine 可以將數(shù)據(jù)發(fā)送到通道,另一個 goroutine 可以接收數(shù)據(jù)。

以下是一些屬性:

  1. 通道本質上是阻塞的。
  2. A 發(fā)送到通道操作 ch 阻塞直到其他 Goroutine 從通道接收。
  3. 從通道接收操作阻塞直到其他goroutine發(fā)送到通道。
package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now()

    downloadFile("file1.txt")
    downloadFile("file2.txt")
    downloadFile("file3.txt")

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

Understanding Goroutines and Channels in Golang with Intuitive Visuals

為什么 ch

Understanding Goroutines and Channels in Golang with Intuitive Visuals

讓我們通過添加 goroutine 來解決這個問題

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    // Launch downloads concurrently
    go downloadFile("file1.txt")
    go downloadFile("file2.txt")
    go downloadFile("file3.txt")

    fmt.Println("All downloads completed!")
}

讓我們想象一下:

Understanding Goroutines and Channels in Golang with Intuitive Visuals

這次消息從不同的 Goroutine 發(fā)送,因此主 Goroutine 在發(fā)送到通道時不會被阻塞,因此它會移動到 msg :=

使用通道修復 main 不等待其他問題

現(xiàn)在讓我們使用channel來修復文件下載器問題(main不等待其他人完成)。

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now() // Record start time

    go downloadFile("file1.txt")
    go downloadFile("file2.txt")
    go downloadFile("file3.txt")

    // Wait for goroutines to finish
    time.Sleep(3 * time.Second)

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

Understanding Goroutines and Channels in Golang with Intuitive Visuals

可視化它:

Understanding Goroutines and Channels in Golang with Intuitive Visuals

讓我們進行一次演練以更好地理解:

節(jié)目開始

主協(xié)程創(chuàng)建完成通道
啟動三個下載 goroutine
每個 goroutine 都會獲得對同一通道的引用

下載執(zhí)行:

  1. 所有三個下載同時運行
  2. 每個需要2秒
  3. 他們可能會以任何順序完成

頻道循環(huán):

  1. 主協(xié)程進入循環(huán): for i := 0;我
  2. 每個
  3. 循環(huán)確保我們等待所有三個完成信號

Understanding Goroutines and Channels in Golang with Intuitive Visuals

循環(huán)行為:

  1. 迭代 1:阻塞直至首次下載完成
  2. 迭代 2:阻塞直到第二次下載完成
  3. 迭代 3:阻塞直至最終下載完成

完成順序并不重要!

觀察
? 每次發(fā)送(完成 ? 主協(xié)程通過循環(huán)協(xié)調一切

兩個goroutine如何通信?

我們已經了解了兩個 goroutine 如何進行通信。什么時候?一直以來。 我們不要忘記 main 函數(shù)也是一個 goroutine。

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now()

    downloadFile("file1.txt")
    downloadFile("file2.txt")
    downloadFile("file3.txt")

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

讓我們想象一下并試運行一下:

Understanding Goroutines and Channels in Golang with Intuitive Visuals

試運行:

程序開始(t=0ms)

  • 主協(xié)程初始化三個通道:
    • ch:用于消息。
    • senderDone:表示發(fā)送者完成。
    • receiveDone:表示接收器完成。
  • 主協(xié)程啟動兩個協(xié)程:
    • 發(fā)件人。
    • 接收器。
  • 主 Goroutine 塊,等待來自

第一條消息(t=1ms)

  1. 發(fā)送者將“消息1”發(fā)送到ch通道。
  2. 接收者醒來并處理消息:
    • 打?。?strong>“已收到:消息 1”。
  3. 發(fā)送者休眠 100 毫秒。

第二條消息(t=101ms)

  1. 發(fā)送者醒來并向ch通道發(fā)送“消息2”。
  2. 接收方處理消息:
    • 打?。?strong>“已收到:消息 2”。
  3. 發(fā)送者再休眠 100 毫秒。

第三條消息(t=201ms)

  1. 發(fā)送者醒來并向ch通道發(fā)送“消息3”。
  2. 接收方處理消息:
    • 打印:“已收到:消息 3”。
  3. 發(fā)送者最后一次睡覺。

通道關閉(t=301ms)

  1. 發(fā)送者完成睡眠并關閉 ch 通道。
  2. 發(fā)送者向 senderDone 通道發(fā)送一個 true 信號以指示完成。
  3. 接收器檢測到ch通道已關閉。
  4. 接收器退出其 for-range 循環(huán)。

完成(t=302-303ms)

  1. 主協(xié)程收到來自senderDone的信號并停止等待。
  2. 主協(xié)程開始等待來自 receiveDone 的信號。
  3. 接收器向receiverDone通道發(fā)送完成信號。
  4. 主協(xié)程接收信號并打印:
    • “所有操作完成!”.
  5. 程序退出。

緩沖通道

為什么我們需要緩沖通道?
無緩沖的通道會阻塞發(fā)送方和接收方,直到另一方準備好為止。當需要高頻通信時,無緩沖的通道可能會成為瓶頸,因為兩個 goroutine 必須暫停來交換數(shù)據(jù)。

緩沖通道屬性:

  1. FIFO(先進先出,類似于隊列)
  2. 固定大小,在創(chuàng)建時設置
  3. 緩沖區(qū)已滿時阻止發(fā)送者
  4. 當緩沖區(qū)為空時阻塞接收器

Understanding Goroutines and Channels in Golang with Intuitive Visuals

我們看到它的實際效果:

package main

import (
    "fmt"
    "time"
)

func downloadFile(filename string) {
    fmt.Printf("Starting download: %s\n", filename)
    // Simulate file download with sleep
    time.Sleep(2 * time.Second)
    fmt.Printf("Finished download: %s\n", filename)
}

func main() {
    fmt.Println("Starting downloads...")

    startTime := time.Now()

    downloadFile("file1.txt")
    downloadFile("file2.txt")
    downloadFile("file3.txt")

    elapsedTime := time.Since(startTime)

    fmt.Printf("All downloads completed! Time elapsed: %s\n", elapsedTime)
}

輸出(在取消注釋 ch

Understanding Goroutines and Channels in Golang with Intuitive Visuals

為什么它沒有阻塞主協(xié)程?

  1. 緩沖通道允許發(fā)送至其容量而不阻塞發(fā)送者。

  2. 通道的容量為 2,這意味著它在阻塞之前可以在緩沖區(qū)中保存兩個值。

  3. 緩沖區(qū)已經滿了“第一”和“第二”。由于沒有并發(fā)接收者來使用這些值,因此發(fā)送操作會無限期地阻塞。

  4. 因為主 goroutine 也負責發(fā)送,并且沒有其他活動的 goroutine 從通道接收值,所以程序在嘗試發(fā)送第三條消息時陷入死鎖。

取消注釋第三條消息會導致死鎖,因為容量現(xiàn)在已滿,第三條消息將阻塞,直到緩沖區(qū)釋放。

Understanding Goroutines and Channels in Golang with Intuitive Visuals

何時使用緩沖通道與非緩沖通道

Aspect Buffered Channels Unbuffered Channels
Purpose For decoupling sender and receiver timing. For immediate synchronization between sender and receiver.
When to Use - When the sender can proceed without waiting for receiver. - When sender and receiver must synchronize directly.
- When buffering improves performance or throughput. - When you want to enforce message-handling immediately.
Blocking Behavior Blocks only when buffer is full. Sender blocks until receiver is ready, and vice versa.
Performance Can improve performance by reducing synchronization. May introduce latency due to synchronization.
Example Use Cases - Logging with rate-limited processing. - Simple signaling between goroutines.
- Batch processing where messages are queued temporarily. - Hand-off of data without delay or buffering.
Complexity Requires careful buffer size tuning to avoid overflows. Simpler to use; no tuning needed.
Overhead Higher memory usage due to the buffer. Lower memory usage; no buffer involved.
Concurrency Pattern Asynchronous communication between sender and receiver. Synchronous communication; tight coupling.
Error-Prone Scenarios Deadlocks if buffer size is mismanaged. Deadlocks if no goroutine is ready to receive or send.

要點

使用 緩沖 通道如果:

  1. 你需要解耦發(fā)送者和接收者的時間。
  2. 批處理或排隊消息可以提高性能。
  3. 當緩沖區(qū)已滿時,應用程序可以容忍處理消息的延遲。

使用 無緩沖 通道,如果:

  1. goroutines 之間的同步至關重要。
  2. 您想要簡單并立即傳遞數(shù)據(jù)。
  3. 發(fā)送者和接收者之間的交互必須即時發(fā)生。

這些基礎知識為更高級的概念奠定了基礎。在我們即將發(fā)布的帖子中,我們將探討:

下一篇文章:

  1. 并發(fā)模式
  2. 互斥和內存同步

請繼續(xù)關注我們,我們將繼續(xù)加深對 Go 強大并發(fā)功能的理解!

Understanding Goroutines and Channels in Golang with Intuitive Visuals

以上是通過直觀的視覺效果了解 Golang 中的 Goroutines 和 Channel的詳細內容。更多信息請關注PHP中文網(wǎng)其他相關文章!

本站聲明
本文內容由網(wǎng)友自發(fā)貢獻,版權歸原作者所有,本站不承擔相應法律責任。如您發(fā)現(xiàn)有涉嫌抄襲侵權的內容,請聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅動的應用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機

Video Face Swap

Video Face Swap

使用我們完全免費的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
是Golang前端還是后端 是Golang前端還是后端 Jul 08, 2025 am 01:44 AM

Golang主要用于后端開發(fā),但也能在前端領域間接發(fā)揮作用。其設計目標聚焦高性能、并發(fā)處理和系統(tǒng)級編程,適合構建API服務器、微服務、分布式系統(tǒng)、數(shù)據(jù)庫操作及CLI工具等后端應用。雖然Golang不是網(wǎng)頁前端的主流語言,但可通過GopherJS編譯成JavaScript、通過TinyGo運行于WebAssembly,或搭配模板引擎生成HTML頁面來參與前端開發(fā)。然而,現(xiàn)代前端開發(fā)仍需依賴JavaScript/TypeScript及其生態(tài)。因此,Golang更適合以高性能后端為核心的技術棧選擇。

如何在Golang中構建GraphQl API 如何在Golang中構建GraphQl API Jul 08, 2025 am 01:03 AM

要構建一個GraphQLAPI在Go語言中,推薦使用gqlgen庫以提高開發(fā)效率。1.首先選擇合適的庫,如gqlgen,它支持根據(jù)schema自動生成代碼;2.接著定義GraphQLschema,描述API的結構和查詢入口,如定義Post類型和查詢方法;3.然后初始化項目并生成基礎代碼,實現(xiàn)resolver中的業(yè)務邏輯;4.最后將GraphQLhandler接入HTTPserver,通過內置Playground測試API。注意事項包括字段命名規(guī)范、錯誤處理、性能優(yōu)化及安全設置等,確保項目可維護性

如何安裝去 如何安裝去 Jul 09, 2025 am 02:37 AM

安裝Go的關鍵在于選擇正確版本、配置環(huán)境變量并驗證安裝。1.前往官網(wǎng)下載對應系統(tǒng)的安裝包,Windows使用.msi文件,macOS使用.pkg文件,Linux使用.tar.gz文件并解壓至/usr/local目錄;2.配置環(huán)境變量,在Linux/macOS中編輯~/.bashrc或~/.zshrc添加PATH和GOPATH,Windows則在系統(tǒng)屬性中設置PATH為Go的安裝路徑;3.使用goversion命令驗證安裝,并運行測試程序hello.go確認編譯執(zhí)行正常。整個流程中PATH設置和環(huán)

Go Sync.WaitGroup示例 Go Sync.WaitGroup示例 Jul 09, 2025 am 01:48 AM

sync.WaitGroup用于等待一組goroutine完成任務,其核心是通過Add、Done、Wait三個方法協(xié)同工作。1.Add(n)設置需等待的goroutine數(shù)量;2.Done()在每個goroutine結束時調用,計數(shù)減一;3.Wait()阻塞主協(xié)程直到所有任務完成。使用時需注意:Add應在goroutine外調用、避免重復Wait、務必確保Done被調用,推薦配合defer使用。常見于并發(fā)抓取網(wǎng)頁、批量數(shù)據(jù)處理等場景,能有效控制并發(fā)流程。

進行音頻/視頻處理 進行音頻/視頻處理 Jul 20, 2025 am 04:14 AM

音視頻處理的核心在于理解基本流程與優(yōu)化方法。1.其基本流程包括采集、編碼、傳輸、解碼和播放,每個環(huán)節(jié)均有技術難點;2.常見問題如音畫不同步、卡頓延遲、聲音噪音、畫面模糊等,可通過同步調整、編碼優(yōu)化、降噪模塊、參數(shù)調節(jié)等方式解決;3.推薦使用FFmpeg、OpenCV、WebRTC、GStreamer等工具實現(xiàn)功能;4.性能管理方面應注重硬件加速、合理設置分辨率幀率、控制并發(fā)及內存泄漏問題。掌握這些關鍵點有助于提升開發(fā)效率和用戶體驗。

去嵌入軟件包教程 去嵌入軟件包教程 Jul 09, 2025 am 02:46 AM

使用Go的embed包可以方便地將靜態(tài)資源嵌入二進制,適合Web服務打包HTML、CSS、圖片等文件。1.聲明嵌入資源需在變量前加//go:embed注釋,如嵌入單個文件hello.txt;2.可嵌入整個目錄如static/*,通過embed.FS實現(xiàn)多文件打包;3.開發(fā)時建議通過buildtag或環(huán)境變量切換磁盤加載模式以提高效率;4.注意路徑正確性、文件大小限制及嵌入資源的只讀特性。合理使用embed能簡化部署并優(yōu)化項目結構。

如何在GO中構建Web服務器 如何在GO中構建Web服務器 Jul 15, 2025 am 03:05 AM

搭建一個用Go編寫的Web服務器并不難,核心在于利用net/http包實現(xiàn)基礎服務。1.使用net/http啟動最簡服務器:通過幾行代碼注冊處理函數(shù)并監(jiān)聽端口;2.路由管理:使用ServeMux組織多個接口路徑,便于結構化管理;3.常見做法:按功能模塊分組路由,并可用第三方庫支持復雜匹配;4.靜態(tài)文件服務:通過http.FileServer提供HTML、CSS和JS文件;5.性能與安全:啟用HTTPS、限制請求體大小、設置超時時間以提升安全性與性能。掌握這些要點后,擴展功能將更加容易。

使用默認情況選擇 使用默認情況選擇 Jul 14, 2025 am 02:54 AM

select加default的作用是讓select在沒有其他分支就緒時執(zhí)行默認行為,避免程序阻塞。1.非阻塞地從channel接收數(shù)據(jù)時,若channel為空,會直接進入default分支;2.結合time.After或ticker定時嘗試發(fā)送數(shù)據(jù),若channel滿則不阻塞而跳過;3.防止死鎖,在不確定channel是否被關閉時避免程序卡??;使用時需注意default分支會立即執(zhí)行,不能濫用,且default與case互斥,不會同時執(zhí)行。

See all articles