1。運行每個範例:不要只閱讀程式碼。輸入它,運行它,然後觀察其行為。 ⚠️ 這個系列如何進行?
2。實驗和打破常規: 刪除睡眠並看看會發生什麼,更改通道緩衝區大小,修改 goroutine 計數。
打破東西會教你它們是如何運作的
3。關於行為的原因: 在執行修改後的程式碼之前,嘗試預測結果。當您看到意外行為時,請停下來思考原因。挑戰解釋。
4。建立心理模型:每個視覺化代表一個概念。嘗試為修改後的程式碼繪製自己的圖表。
這是「掌握 Go 並發」系列的 第 1 部分,我們將介紹:
- goroutine 的工作原理及其生命週期
- goroutines 之間的通道通訊
- 緩衝通道及其用例
- 實際範例與視覺化
我們將從基礎知識開始,逐步發展如何有效使用它們的直覺。
這會有點長,相當長,所以做好準備。
我們將親力親為完成整個過程。
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) }
程式總共需要 6 秒,因為每個 2 秒的下載必須在下一個開始之前完成。讓我們想像一下:
我們可以縮短這個時間,讓我們修改我們的程式以使用go例程:
注意:函數呼叫前使用 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!") }
等等什麼?沒有列印任何內容?為什麼?
讓我們想像一下這一點,以了解可能發生的情況。
從上面的視覺化中,我們了解到 main 函數在 goroutine 完成之前就存在了。一項觀察結果是,所有 goroutine 的生命週期都依賴 main 函數。
注意:main函數本身就是一個goroutine;)
為了解決這個問題,我們需要一種方法讓主 goroutine 等待其他 goroutine 完成。有幾種方法可以做到這一點:
- 等待幾秒鐘(駭客方式)
- 使用 WaitGroup(正確的方法,下一步)
- 使用頻道(我們將在下面介紹)
讓我們等待幾秒鐘讓 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是一種並發控制機制,用於等待一組goroutines執行完成。
在這裡讓我們看看它的實際效果並視覺化:
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!") }
讓我們可視化這一點並了解sync.WaitGroup 的工作原理:
計數器機制:
- WaitGroup 維護一個內部計數器
- wg.Add(n) 將計數器增加 n
- wg.Done() 將計數器減 1
- wg.Wait() 阻塞,直到計數器達到 0
同步流程:
- 主 Goroutine 在啟動 Goroutines 之前呼叫 Add(3)
- 每個 goroutine 完成時都會呼叫 Done()
- 主協程在 Wait() 處被阻塞,直到計數器達到 0
- 當計數器達到 0 時,程式繼續並乾淨地退出
要避免的常見陷阱
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 例程如何通訊?這就是頻道發揮作用的地方。
Go 中的Channels 是一個強大的並發原語,用於 goroutine 之間的通信。它們為 goroutine 提供了一種安全共享資料的方法。
將通道視為管道:一個 goroutine 可以將資料傳送到通道,另一個 goroutine 可以接收資料。
以下是一些屬性:
- 通道本質上是阻塞的。
- A 送到通道操作 ch 阻塞直到其他 Goroutine 從通道接收。
- 從通道接收操作阻塞直到其他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) }
為什麼 ch
讓我們透過加入 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!") }
讓我們想像一下:
這次訊息從不同的Goroutine 發送,因此主Goroutine 在發送到通道時不會被阻塞,因此它會移動到msg :=
使用通道修復 main 不等其他問題
現在讓我們使用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) }
可視化它:
讓我們進行一次演練以更好地理解:
節目開始:
主協程建立完成通道
啟動三個下載 goroutine
每個 goroutine 都會獲得對同一通道的引用
下載執行:
- 所有三個下載同時運行
- 每個需要2秒
- 他們可能會以任何順序完成
頻道循環:
- 主協程進入迴圈: for i := 0;我
- 每個
- 循環確保我們等待所有三個完成訊號
循環行為:
- 迭代 1:阻塞至首次下載完成
- 迭代 2:阻塞直到第二次下載完成
- 迭代 3:阻塞至最終下載完成
完成順序並不重要!
觀察:
⭐ 每次發送(完成 ⭐ 主協程透過循環協調一切
兩個goroutine如何通信?
我們已經了解了兩個 goroutine 如何進行溝通。什麼時候?一直以來。 我們不要忘記 main 函數也是一個 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) }
讓我們想像一下並試運行一下:
試運轉:
程式開始(t=0ms)
第一則訊息(t=1ms)
第二則訊息(t=101ms)
第三則訊息(t=201ms)
通道關閉(t=301ms)
完成(t=302-303ms)
緩衝通道
為什麼我們需要緩衝通道?
無緩衝的通道會阻塞傳送方和接收方,直到另一方準備好為止。當需要高頻通訊時,無緩衝的通道可能會成為瓶頸,因為兩個 goroutine 必須暫停來交換資料。
緩衝通道屬性:
- FIFO(先進先出,類似隊列)
- 固定大小,建立時設定
- 緩衝區已滿時阻止發送者
- 當緩衝區為空時阻塞接收器
我們看到它的實際效果:
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
為什麼它沒有阻塞主協程?
緩衝通道允許發送至其容量而不阻塞發送者。
通道的容量為 2,這表示它在阻塞之前可以在緩衝區中保存兩個值。
緩衝區已經滿了「第一」和「第二」。由於沒有並發接收者來使用這些值,因此發送操作會無限期地阻塞。
因為主 goroutine 也負責發送,並且沒有其他活動的 goroutine 從通道接收值,所以程式在嘗試發送第三條訊息時陷入死鎖。
取消註解第三則訊息會導致死鎖,因為容量現在已滿,第三則訊息將阻塞,直到緩衝區釋放。
何時使用緩衝通道與非緩衝通道
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. |
重點
使用 緩衝 通道如果:
- 你需要解耦發送者和接收者的時間。
- 批次或排隊訊息可以提高效能。
- 當緩衝區已滿時,應用程式可以容忍處理訊息的延遲。
使用 無緩衝 通道,如果:
- goroutines 之間的同步至關重要。
- 您想要簡單並立即傳遞資料。
- 傳送者和接收者之間的互動必須即時發生。
這些基礎知識為更高階的概念奠定了基礎。在我們即將發布的帖子中,我們將探討:
下一篇:
- 並發模式
- 互斥與記憶體同步
請繼續關注我們,我們將繼續加深對 Go 強大並發功能的理解!
以上是透過直覺的視覺效果了解 Golang 中的 Goroutines 和 Channel的詳細內容。更多資訊請關注PHP中文網其他相關文章!

掌握Go語言中的strings包可以提高文本處理能力和開發效率。 1)使用Contains函數檢查子字符串,2)用Index函數查找子字符串位置,3)Join函數高效拼接字符串切片,4)Replace函數替換子字符串。注意避免常見錯誤,如未檢查空字符串和大字符串操作性能問題。

你應該關心Go語言中的strings包,因為它能簡化字符串操作,使代碼更清晰高效。 1)使用strings.Join高效拼接字符串;2)用strings.Fields按空白符分割字符串;3)通過strings.Index和strings.LastIndex查找子串位置;4)用strings.ReplaceAll進行字符串替換;5)利用strings.Builder進行高效字符串拼接;6)始終驗證輸入以避免意外結果。

thestringspackageingoisesential forefficientstringManipulation.1)itoffersSimpleyetpoperfulfunctionsFortaskSlikeCheckingSslingSubstringsStringStringsStringsandStringsN.2)ithandhishiCodeDewell,withFunctionsLikestrings.fieldsfieldsfieldsfordsforeflikester.fieldsfordsforwhitespace-fieldsforwhitespace-separatedvalues.3)3)

WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

Go的strings包提供了多種字符串操作功能。 1)使用strings.Contains檢查子字符串。 2)用strings.Split將字符串分割成子字符串切片。 3)通過strings.Join合併字符串。 4)用strings.TrimSpace或strings.Trim去除字符串首尾的空白或指定字符。 5)用strings.ReplaceAll替換所有指定子字符串。 6)使用strings.HasPrefix或strings.HasSuffix檢查字符串的前綴或後綴。

使用Go語言的strings包可以提升代碼質量。 1)使用strings.Join()優雅地連接字符串數組,避免性能開銷。 2)結合strings.Split()和strings.Contains()處理文本,注意大小寫敏感問題。 3)避免濫用strings.Replace(),考慮使用正則表達式進行大量替換。 4)使用strings.Builder提高頻繁拼接字符串的性能。

Go的bytes包提供了多種實用的函數來處理字節切片。 1.bytes.Contains用於檢查字節切片是否包含特定序列。 2.bytes.Split用於將字節切片分割成smallerpieces。 3.bytes.Join用於將多個字節切片連接成一個。 4.bytes.TrimSpace用於去除字節切片的前後空白。 5.bytes.Equal用於比較兩個字節切片是否相等。 6.bytes.Index用於查找子切片在largerslice中的起始索引。

theEncoding/binarypackageingoisesenebecapeitProvidesAstandArdArdArdArdArdArdArdArdAndWriteBinaryData,確保Cross-cross-platformCompatibilitiational and handhandlingdifferentendenness.itoffersfunctionslikeread,寫下,寫,dearte,readuvarint,andwriteuvarint,andWriteuvarIntforPreciseControloverBinary


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境