Home > Article > Backend Development > Project practice: Using Go WaitGroup to improve Golang concurrency performance
Practical Project: Using Go WaitGroup to Improve Golang Concurrency Performance
Abstract:
In today's Internet era, high concurrency processing has become an issue that cannot be ignored in the development of various systems. question. As a programming language that supports efficient concurrency, Golang has rich concurrent programming features. This article introduces how to use Go WaitGroup to improve Golang's concurrency performance through actual project scenarios. Using specific code examples as clues, readers are guided to use WaitGroup to implement concurrent task processing in actual combat.
Keywords:
Golang; Concurrency performance; WaitGroup; Practical combat; Code examples
3.1 Initialize WaitGroup
In the main thread, you first need to initialize a WaitGroup object. The WaitGroup object counts the number of waiting tasks through the Add method.
var wg sync.WaitGroup
3.2 Adding tasks
In concurrent tasks, specific tasks need to be performed in goroutine, and after the task is completed, the task is marked as completed through the Done method.
wg.Add(1) // 增加一个任务计数 go func() { defer wg.Done() // 标记任务完成 // 执行具体任务 }()
3.3 Waiting for task completion
The main thread waits for all tasks to be completed by calling the Wait method.
wg.Wait()
package main import ( "fmt" "io/ioutil" "net/http" "sync" ) func main() { urls := []string{"https://www.example.com/1", "https://www.example.com/2", "https://www.example.com/3"} var wg sync.WaitGroup for _, url := range urls { wg.Add(1) go func(url string) { defer wg.Done() resp, err := http.Get(url) if err != nil { fmt.Printf("Failed to fetch %s: %v ", url, err) return } body, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { fmt.Printf("Failed to read response body from %s: %v ", url, err) return } err = ioutil.WriteFile("data/"+url, body, 0644) if err != nil { fmt.Printf("Failed to write file for %s: %v ", url, err) } }(url) } wg.Wait() fmt.Println("All tasks completed.") }
In the above sample code, we use WaitGroup to implement concurrent processing of downloading data from multiple URLs. Increase the task count through the Add method, mark the task completion through the Done method, and wait for all task execution to be completed through the Wait method. In this way, we can handle concurrent tasks efficiently.
Reference:
【Article word count: 495】
The above is the detailed content of Project practice: Using Go WaitGroup to improve Golang concurrency performance. For more information, please follow other related articles on the PHP Chinese website!