{"id":13394020,"url":"https://github.com/rakyll/coop","last_synced_at":"2025-09-27T10:31:20.233Z","repository":{"id":13283005,"uuid":"15968733","full_name":"rakyll/coop","owner":"rakyll","description":"Cheat sheet for some of the common concurrent flows in Go","archived":true,"fork":false,"pushed_at":"2015-10-06T15:47:03.000Z","size":234,"stargazers_count":1208,"open_issues_count":5,"forks_count":46,"subscribers_count":35,"default_branch":"master","last_synced_at":"2024-09-21T17:04:55.797Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rakyll.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-16T13:37:46.000Z","updated_at":"2024-08-17T13:49:30.000Z","dependencies_parsed_at":"2022-09-13T08:00:32.047Z","dependency_job_id":null,"html_url":"https://github.com/rakyll/coop","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakyll%2Fcoop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakyll%2Fcoop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakyll%2Fcoop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rakyll%2Fcoop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rakyll","download_url":"https://codeload.github.com/rakyll/coop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871974,"owners_count":16554475,"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":[],"created_at":"2024-07-30T17:01:06.017Z","updated_at":"2025-09-27T10:31:14.998Z","avatar_url":"https://github.com/rakyll.png","language":"Go","funding_links":[],"categories":["Programming Languages","Go","Utilities","實用工具","实用工具"],"sub_categories":["Advanced Console UIs","高級控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","高级控制台界面"],"readme":"# coop\n\n[![Build Status](https://travis-ci.org/rakyll/coop.png?branch=master)](https://travis-ci.org/rakyll/coop)\n\nNote: This package became obsolete. I started it when I was learning Go a couple of years ago. I see so many better ways to implement them all now, so don't keep using this package as an ultimate reference.\n\ncoop contains some of the most common concurrent program flows I personally use in Go. I'm suggesting you to use this package as a snippets reference/cheat sheet instead of a library. The functionally provided in this package can be obtained in many different ways, and frankly with more performant implementations depending on the type of your problem.\n\ncoop contains implementations for the following flows:\n\n### coop.At(time, fn)\n\nRuns fn at t, returns a boolean channel that will receive a message after fn returns. The following example, prints \"Hello World\" in a minute and blocks the goroutine its running in until fn is completed.\n\n~~~ go\ndone := coop.At(time.Now().Add(time.Minute), func() {\n    fmt.Println(\"Hello world\")\n})\n\u003c-done // wait for fn to be done\n~~~\n\n### coop.Until(time, duration, fn)\n\nRuns fn once in every provided duration until t, returns a boolean channel that will receive a message after fn returns. The following example prints \"Hello world\" every minute until tomorrow, and blocks the goroutine its running in until the job is completed.\n\n~~~ go\ndone := coop.Until(time.Now().Add(24*time.Hour), time.Minute, func() {\n    fmt.Println(\"Hello world\")\n})\n\u003c-done\n~~~\n\n### coop.After(duration, fn)\n\nRuns fn after duration, returns a boolean channel that will receive a message after fn returns. The following example prints \"Hello world\" after a second and blocks until fn is completed.\n\n~~~ go\ndone := coop.After(time.Second, func() {\n    fmt.Println(\"Hello world\")\n})\n\u003c-done\n~~~\n\n### coop.Every(duration, fn)\n\nRuns fn once in every duration, and never stops. The following example will print \"Hello World\" once in every second.\n\n~~~ go\ncoop.Every(time.Second, func() {\n    fmt.Println(\"Hello world\")\n})\n~~~\n\n### coop.Timeout(duration, fn)\nRuns fn, and cancels the running job if timeout is exceeded. The following example will timeout and fn will return immediately (\"Hello world will not printed\"), the value read from the done channel will be false if timeout occurs, true if fn is completed.\n\n~~~ go\ndone := coop.Timeout(time.Second, func() {\n    time.Sleep(time.Hour)\n    fmt.Println(\"Hello world\")\n})\n\u003c-done // will return false, because timeout occurred\n~~~\n\n### coop.All(fns...)\nRuns the list of fns concurrently, returns a boolean channel that will receive a message after all of the fns are completed. The following example will start 4 printing jobs concurrently and wait until all of them are completed.\n\n~~~ go\nprintFn := func() {\n    fmt.Println(\"Hello world\")\n}\n\u003c-coop.All(printFn, printFn, printFn, printFn)\n~~~\n\n### coop.AllWithThrottle(num, fns...)\nSimilar to coop.All, but with limiting. Runs the list of fns concurrently, but at most num fns at a time. Returns a boolean channel that will receive a message after all of the fns are completed. The following example will start 3 printing jobs immediately, and run the left out one once the first 3 is completed. It will block the goroutine until all 4 are finished.\n\n~~~ go\nprintFn := func() {\n    fmt.Println(\"Hello world\")\n}\n\u003c-coop.AllWithThrottle(3, printFn, printFn, printFn, printFn)\n~~~\n\n### coop.Replicate(n, fn)\n\nRuns fn n time concurrently, returns a boolean channel that indicates all runs are completed. The following example prints \"Hello world\" 5 times, and waits for all printing jobs are finished.\n\n~~~ go\n\u003c-coop.Replicate(5, func() {\n    fmt.Println(\"Hello world\")\n})\n~~~\n\n## License\n\nCopyright 2014 Google Inc. All Rights Reserved.\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ![Analytics](https://ga-beacon.appspot.com/UA-46881978-1/coop?pixel)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frakyll%2Fcoop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frakyll%2Fcoop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frakyll%2Fcoop/lists"}