{"id":183,"date":"2026-04-11T09:00:00","date_gmt":"2026-04-11T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/?p=183"},"modified":"2026-10-10T21:20:44","modified_gmt":"2026-10-10T15:50:44","slug":"go-sync-package-waitgroup-mutex","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/","title":{"rendered":"Go Sync Package: WaitGroup and Mutex Explained"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Go sync package gives you simple tools to keep goroutines from stepping on each other&#8217;s data. In this post, you will learn what a data race is and how Mutex, WaitGroup, RWMutex and Once fix real problems. Each idea comes with a short example that you can run yourself.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on the last two. So if goroutines or channels still feel new, first read <a href=\"https:\/\/smitabharat.com\/blog\/go-goroutines-beginners-guide\/\">goroutines in Go for beginners<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-channels-examples\/\">channels in Go with examples<\/a>. We will also use pointer receivers from <a href=\"https:\/\/smitabharat.com\/blog\/go-structs-beginners-guide\/\">structs and methods in Go<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the Go Sync Package?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>sync<\/code> package is part of the standard library. In particular, it offers small building blocks for coordinating goroutines, such as locks and counters. You need no install, just <code>import \"sync\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, think of a single shared bathroom with a lock on the door. Only one person goes in at a time, and everyone else waits. The Go sync package gives your goroutines that same lock, so shared data stays safe.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Problem the Go Sync Package Solves: Data Races<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>var wg sync.WaitGroup\ncount := 0\n\nfor i := 0; i &lt; 1000; i++ {\n\twg.Add(1)\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tcount++\n\t}()\n}\nwg.Wait()\nfmt.Println(count) \/\/ often less than 1000, for example 997<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A data race happens when two goroutines change the same variable at the same time. Here <code>count++<\/code> reads, adds and writes back in three steps. So two goroutines can read the same old value, and one update gets lost.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Spotting Races with -race<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>go run -race main.go\n# WARNING: DATA RACE<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Fortunately, the <code>-race<\/code> flag turns on Go&#8217;s race detector. Then it watches memory while your program runs and reports any unsafe access. Use it in tests, because races often hide on fast machines.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protecting Data with sync.Mutex<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>type Counter struct {\n\tmu    sync.Mutex\n\tvalue int\n}\n\nfunc (c *Counter) Inc() {\n\tc.mu.Lock()\n\tdefer c.mu.Unlock()\n\tc.value++\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So a mutex, short for mutual exclusion, lets only one goroutine into a section of code at a time. The diagram below compares the race with the locked version.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-diagram-1024x640.png\" alt=\"Go Sync Package diagram showing a data race losing an update next to a mutex keeping the count correct\" class=\"wp-image-185\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-diagram-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-diagram-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-diagram-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-diagram.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Without a lock, two goroutines lose an update. With a mutex, they take turns and the count is correct.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lock, Unlock and defer<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>Lock()<\/code> waits until the mutex is free and then takes it. Next, <code>defer Unlock()<\/code> makes sure the method releases it when it ends. Now 1,000 goroutines calling <code>c.Inc()<\/code> always give exactly 1000.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Waiting with sync.WaitGroup<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>var wg sync.WaitGroup\nvar c Counter\n\nfor i := 0; i &lt; 1000; i++ {\n\twg.Add(1)\n\tgo func() {\n\t\tdefer wg.Done()\n\t\tc.Inc()\n\t}()\n}\nwg.Wait()\nfmt.Println(c.value) \/\/ 1000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You met WaitGroup in the goroutines lesson. In short, it counts running tasks, while the mutex protects shared data. Together they are the most common pair in the Go sync package.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Other Go Sync Package Tools<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">sync.RWMutex for Many Readers<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>type Cache struct {\n\tmu   sync.RWMutex\n\tdata map&#91;string]string\n}\n\nfunc (c *Cache) Get(key string) string {\n\tc.mu.RLock()\n\tdefer c.mu.RUnlock()\n\treturn c.data&#91;key]\n}\n\nfunc (c *Cache) Set(key, value string) {\n\tc.mu.Lock()\n\tdefer c.mu.Unlock()\n\tc.data&#91;key] = value\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">An RWMutex lets many goroutines read at once, but only one write at a time. For instance, this suits caches, where reads happen far more often than writes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">sync.Once for One-Time Setup<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>var once sync.Once\nfor i := 0; i &lt; 3; i++ {\n\tonce.Do(func() { fmt.Println(\"setup runs once\") })\n}\n\/\/ setup runs once<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, Once runs a function exactly one time, even if many goroutines call it. For example, use it to open a database connection the first time someone needs it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Mutex or Channel: When to Use the Go Sync Package<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Situation<\/th><th>Best tool<\/th><\/tr><\/thead><tbody><tr><td>Protect a shared counter, map or cache<\/td><td>sync.Mutex or RWMutex<\/td><\/tr><tr><td>Pass data or work from one goroutine to another<\/td><td>Channel<\/td><\/tr><tr><td>Wait for a group of goroutines to finish<\/td><td>sync.WaitGroup<\/td><\/tr><tr><td>Run setup code only once<\/td><td>sync.Once<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As a simple rule, use channels to move data and a mutex to guard data. Both are correct Go, so pick whichever makes the code easier to read.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with the Go Sync Package<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error you see<\/th><th>Why it happens<\/th><th>How to fix it<\/th><\/tr><\/thead><tbody><tr><td>fatal error: sync: unlock of unlocked mutex<\/td><td>Unlock ran without a Lock.<\/td><td>Pair every Lock with one Unlock.<\/td><\/tr><tr><td>show passes lock by value (from go vet)<\/td><td>A struct with a mutex was copied.<\/td><td>Pass a pointer instead.<\/td><\/tr><tr><td>Program freezes<\/td><td>The code locked twice without unlocking.<\/td><td>Use defer mu.Unlock().<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Copying a mutex is a quiet bug, since each copy has its own lock. Therefore, run <code>go vet<\/code> to catch it early. Concurrency questions are interview favourites, so practise with my <a href=\"https:\/\/smitabharat.com\/blog\/go-lang-interview-questions-and-answers\/\">Go interview questions and answers<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages come straight from the Go team, so they are always accurate and up to date.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/sync\">sync package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/tour\/concurrency\/9\">A Tour of Go: sync.Mutex<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/articles\/race_detector\">Go docs: Data Race Detector<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The Go sync package helps goroutines share data safely.<\/li>\n\n\n\n<li>A data race loses updates, and go run -race finds it.<\/li>\n\n\n\n<li>Mutex lets one goroutine at a time into a section, with defer Unlock.<\/li>\n\n\n\n<li>WaitGroup waits for tasks, RWMutex allows many readers and Once runs setup once.<\/li>\n\n\n\n<li>Never copy a struct that holds a mutex.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can now run and coordinate goroutines safely. Next, we will learn how to cancel slow work and set deadlines with the context package.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Coming soon: Go Context Explained for Beginners<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Go sync package gives you simple tools to keep goroutines from stepping on each other&#8217;s data. In this post, you will learn what&hellip;<\/p>\n","protected":false},"author":1,"featured_media":203,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[11,38,23,21],"swapz_series":[],"class_list":["post-183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-lang","tag-go-sync-package","tag-go-tutorial","tag-golang-basics"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Sync Package: WaitGroup and Mutex Explained<\/title>\n<meta name=\"description\" content=\"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Sync Package: WaitGroup and Mutex Explained\" \/>\n<meta property=\"og:description\" content=\"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-04-11T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-10T15:50:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Sync Package: WaitGroup and Mutex Explained\",\"datePublished\":\"2026-04-11T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:50:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/\"},\"wordCount\":750,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sync-package-waitgroup-mutex-featured-1.png\",\"keywords\":[\"Go Lang\",\"Go Sync Package\",\"Go Tutorial\",\"Golang Basics\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/\",\"name\":\"Go Sync Package: WaitGroup and Mutex Explained\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sync-package-waitgroup-mutex-featured-1.png\",\"datePublished\":\"2026-04-11T03:30:00+00:00\",\"dateModified\":\"2026-10-10T15:50:44+00:00\",\"description\":\"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sync-package-waitgroup-mutex-featured-1.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-sync-package-waitgroup-mutex-featured-1.png\",\"width\":1200,\"height\":630,\"caption\":\"Go Sync Package featured image: cards showing Mutex, WaitGroup and Once\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-sync-package-waitgroup-mutex\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Sync Package: WaitGroup and Mutex Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Sync Package: WaitGroup and Mutex Explained","description":"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/","og_locale":"en_US","og_type":"article","og_title":"Go Sync Package: WaitGroup and Mutex Explained","og_description":"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.","og_url":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-04-11T03:30:00+00:00","article_modified_time":"2026-10-10T15:50:44+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Sync Package: WaitGroup and Mutex Explained","datePublished":"2026-04-11T03:30:00+00:00","dateModified":"2026-10-10T15:50:44+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/"},"wordCount":750,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png","keywords":["Go Lang","Go Sync Package","Go Tutorial","Golang Basics"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/","url":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/","name":"Go Sync Package: WaitGroup and Mutex Explained","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png","datePublished":"2026-04-11T03:30:00+00:00","dateModified":"2026-10-10T15:50:44+00:00","description":"Learn the Go sync package with simple examples: fix data races with Mutex, wait with WaitGroup, and use RWMutex and Once safely.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-sync-package-waitgroup-mutex-featured-1.png","width":1200,"height":630,"caption":"Go Sync Package featured image: cards showing Mutex, WaitGroup and Once"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-sync-package-waitgroup-mutex\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Sync Package: WaitGroup and Mutex Explained"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":3,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/183\/revisions\/188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/203"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=183"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}