{"id":21303101,"url":"https://github.com/bzon/goconcurrency-notes","last_synced_at":"2025-03-15T18:45:58.954Z","repository":{"id":141940534,"uuid":"139368269","full_name":"bzon/goconcurrency-notes","owner":"bzon","description":null,"archived":false,"fork":false,"pushed_at":"2018-07-01T22:58:17.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T08:28:33.232Z","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/bzon.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-01T22:54:48.000Z","updated_at":"2018-07-01T22:58:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"97a57dca-5d84-428c-a90c-d71905b31e24","html_url":"https://github.com/bzon/goconcurrency-notes","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/bzon%2Fgoconcurrency-notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzon%2Fgoconcurrency-notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzon%2Fgoconcurrency-notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bzon%2Fgoconcurrency-notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bzon","download_url":"https://codeload.github.com/bzon/goconcurrency-notes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243776710,"owners_count":20346353,"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-11-21T15:58:54.136Z","updated_at":"2025-03-15T18:45:58.942Z","avatar_url":"https://github.com/bzon.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# My Concurrency Notes\n\n\u003c!-- vim-markdown-toc GFM --\u003e\n\n* [Normal Loop](#normal-loop)\n* [Using `go` routines in functions](#using-go-routines-in-functions)\n* [Using `sync.WaitGroup` to capture standard output](#using-syncwaitgroup-to-capture-standard-output)\n\n\u003c!-- vim-markdown-toc --\u003e\n\n## Normal Loop\n\nLet us start with a normal loop by running __foo__ and __bar__ functions in a synchronous manner and nothing fancy. Our goal here is how we can improve the program's performance by using go routines and measuring execution time of the program which will be discussed in the proceeding topics.\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\tfoo()\n\tbar()\n}\n\nfunc foo() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Foo:\", i)\n\t}\n}\n\nfunc bar() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Bar:\", i)\n\t}\n}\n```\n\n## Using `go` routines in functions\n\nLet's add the __go__ statement when calling __foo__ and __bar__. By default in the nature of __Go__, running the code below will just exit the program immediately.\n\nWe will add a __WaitGroup__ in the next topic to make the __main__ function wait for __foo__ and __bar__ to complete, and gives us a standard output and measure the execution time.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\nfunc main() {\n\tgo foo()\n\tgo bar()\n}\n\nfunc foo() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Foo:\", i)\n\t}\n}\n\nfunc bar() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Bar:\", i)\n\t}\n}\n```\n\n## Using `sync.WaitGroup` to capture standard output\n\nWith a __WaitGroup__ we will now be able to see the printed outputs for each __foo__ and __bar__ calls. Try running the first code and the last code below and you should notice a significant increase in the execution time. The difference maker is because __foo__ and __bar__ concurrently executed at the same time (parallelism).\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sync\"\n\t\"time\"\n)\n\nvar wg sync.WaitGroup\n\nfunc main() {\n\twg.Add(2)\n\tgo foo()\n\tgo bar()\n\twg.Wait()\n}\n\nfunc foo() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Foo:\", i)\n\t}\n\twg.Done()\n}\n\nfunc bar() {\n\tfor i := 0; i \u003c 30; i++ {\n\t\ttime.Sleep(100 * time.Millisecond)\n\t\tfmt.Println(\"Bar:\", i)\n\t}\n\twg.Done()\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbzon%2Fgoconcurrency-notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbzon%2Fgoconcurrency-notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbzon%2Fgoconcurrency-notes/lists"}