{"id":13413058,"url":"https://github.com/pieterclaerhout/go-waitgroup","last_synced_at":"2025-05-06T23:24:28.190Z","repository":{"id":57483172,"uuid":"144038488","full_name":"pieterclaerhout/go-waitgroup","owner":"pieterclaerhout","description":"A sync.WaitGroup with error handling and concurrency control","archived":false,"fork":false,"pushed_at":"2024-06-24T06:12:39.000Z","size":39,"stargazers_count":46,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:51:53.369Z","etag":null,"topics":["concurrency","golang","goroutine","goroutines","pool","waitgroup"],"latest_commit_sha":null,"homepage":"https://www.yellowduck.be","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pieterclaerhout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-08T16:12:35.000Z","updated_at":"2024-06-22T15:24:59.000Z","dependencies_parsed_at":"2024-10-26T09:09:45.197Z","dependency_job_id":"b1bd9950-09d2-4dac-be4f-005cc92b73c7","html_url":"https://github.com/pieterclaerhout/go-waitgroup","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pieterclaerhout%2Fgo-waitgroup","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pieterclaerhout%2Fgo-waitgroup/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pieterclaerhout%2Fgo-waitgroup/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pieterclaerhout%2Fgo-waitgroup/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pieterclaerhout","download_url":"https://codeload.github.com/pieterclaerhout/go-waitgroup/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252784376,"owners_count":21803666,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrency","golang","goroutine","goroutines","pool","waitgroup"],"created_at":"2024-07-30T20:01:32.923Z","updated_at":"2025-05-06T23:24:28.164Z","avatar_url":"https://github.com/pieterclaerhout.png","language":"Go","funding_links":[],"categories":["Goroutines","Goroutines `goroutines的管理和使用`","Relational Databases"],"sub_categories":["Advanced Console UIs","Search and Analytic Databases","检索及分析资料库","SQL 查询语句构建库"],"readme":"# go-waitgroup\n\n[![Build Status](https://img.shields.io/github/workflow/status/pieterclaerhout/go-waitgroup/Go)](https://github.com/pieterclaerhout/go-waitgroup/actions?query=workflow%3AGo)\n[![Go Report Card](https://goreportcard.com/badge/github.com/pieterclaerhout/go-waitgroup)](https://goreportcard.com/report/github.com/pieterclaerhout/go-waitgroup)\n[![Documentation](https://godoc.org/github.com/pieterclaerhout/go-waitgroup?status.svg)](http://godoc.org/github.com/pieterclaerhout/go-waitgroup)\n[![license](https://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://github.com/pieterclaerhout/go-waitgroup/raw/master/LICENSE)\n[![GitHub version](https://badge.fury.io/gh/pieterclaerhout%2Fgo-waitgroup.svg)](https://badge.fury.io/gh/pieterclaerhout%2Fgo-waitgroup)\n[![GitHub issues](https://img.shields.io/github/issues/pieterclaerhout/go-waitgroup.svg)](https://github.com/pieterclaerhout/go-waitgroup/issues)\n\n## How to use\n\nAn package that allows you to use the constructs of a [`sync.WaitGroup`](https://golang.org/pkg/sync/#WaitGroup) to\ncreate a pool of goroutines and control the concurrency.\n\nUsing it is just like a normal [`sync.WaitGroup`](https://golang.org/pkg/sync/#WaitGroup). The only difference is the initialisation. When you use `waitgroup.NewWaitGroup`, you have the option to specify it's size.\n\nAny `int` which is bigger than `0` will limit the number of concurrent goroutines. If you specify `-1` or `0`, all goroutines will run at once (just like a plain [`sync.WaitGroup`](https://golang.org/pkg/sync/#WaitGroup)).\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"net/http\"\n\n    \"github.com/pieterclaerhout/go-waitgroup\"\n)\n\nfunc main() {\n    \n    urls := []string{\n        \"https://www.easyjet.com/\",\n        \"https://www.skyscanner.de/\",\n        \"https://www.ryanair.com\",\n        \"https://wizzair.com/\",\n        \"https://www.swiss.com/\",\n    }\n\n    wg := waitgroup.NewWaitGroup(3)\n\n\tfor _, url := range urls {\n\t\twg.BlockAdd()\n\t\tgo func(url string) {\n\t\t\tdefer wg.Done()\n\t\t\tfmt.Printf(\"%s: checking\\n\", url)\n\t\t\tres, err := http.Get(url)\n\t\t\tif err != nil {\n\t\t\t\tfmt.Println(\"Error: %v\")\n\t\t\t} else {\n\t\t\t\tdefer res.Body.Close()\n\t\t\t\tfmt.Printf(\"%s: result: %v\\n\", url, err)\n\t\t\t}\n\t\t}(url)\n\t}\n\n    wg.Wait()\n    fmt.Println(\"Finished\")\n\n}\n```\n\n## Using closures\n\nThere is also a way to use function closures to make it even more readable:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/pieterclaerhout/go-waitgroup\"\n)\n\nfunc main() {\n\n\turls := []string{\n\t\t\"https://www.easyjet.com/\",\n\t\t\"https://www.skyscanner.de/\",\n\t\t\"https://www.ryanair.com\",\n\t\t\"https://wizzair.com/\",\n\t\t\"https://www.swiss.com/\",\n\t}\n\n\twg := waitgroup.NewWaitGroup(3)\n\n\tfor _, url := range urls {\n\n\t\turlToCheck := url\n\t\twg.Add(func() {\n\t\t\tfmt.Printf(\"%s: checking\\n\", urlToCheck)\n\t\t\tres, err := http.Get(urlToCheck)\n\t\t\tif err != nil {\n\t\t\t\tfmt.Println(\"Error: %v\")\n\t\t\t} else {\n\t\t\t\tdefer res.Body.Close()\n\t\t\t\tfmt.Printf(\"%s: result: %v\\n\", urlToCheck, err)\n\t\t\t}\n\t\t})\n\n\t}\n\n\twg.Wait()\n\tfmt.Println(\"Finished\")\n\n}\n```\n\n## Handling errors\n\nIf you want to handle errors, there is also an `ErrorGroup`. This uses the same principles as a normal `WaitGroup` with a small twist.\n\nFirst of all, you can only add functions which returns just an `error`.\n\nSecond, as soon as one of the queued items fail, the rest will be cancelled:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/pieterclaerhout/go-waitgroup\"\n)\n\nfunc main() {\n\n\tctx := context.Background()\n\n\twg, ctx := waitgroup.NewErrorGroup(ctx, tc.size)\n\tif err != nil {\n\t\tfmt.Println(\"Error: %v\")\n\t\tos.Exit(1)\n\t}\n\n\twg.Add(func() error {\n\t\treturn nil\n\t})\n\n\twg.Add(func() error {\n\t\treturn errors.New(\"An error occurred\")\n\t})\n\n\tif err := wg.Wait(); err != nil {\n\t\tfmt.Println(\"Error: %v\")\n\t\tos.Exit(1)\n\t}\n\n}\n```\n\nYou can also add multiple functions in one step:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/pieterclaerhout/go-waitgroup\"\n)\n\nfunc main() {\n\n\tctx := context.Background()\n\n\twg, ctx := waitgroup.NewErrorGroup(ctx, tc.size)\n\tif err != nil {\n\t\tfmt.Println(\"Error: %v\")\n\t\tos.Exit(1)\n\t}\n\n\twg.Add(\n\t\tfunc() error {\n\t\t\treturn nil\n\t\t},\n\t\tfunc() error {\n\t\t\treturn errors.New(\"An error occurred\")\n\t\t},\n\t)\n\n\tif err := wg.Wait(); err != nil {\n\t\tfmt.Println(\"Error: %v\")\n\t\tos.Exit(1)\n\t}\n\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpieterclaerhout%2Fgo-waitgroup","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpieterclaerhout%2Fgo-waitgroup","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpieterclaerhout%2Fgo-waitgroup/lists"}