[{"data":1,"prerenderedAt":1204},["ShallowReactive",2],{"navigation_docs":3,"-concurrency-synchronization":377,"-concurrency-synchronization-surround":1199},[4,39,114,144,184,244,293,362],{"title":5,"icon":6,"path":7,"stem":8,"children":9,"page":6},"Getting Started",false,"\u002Fgetting-started","1.getting-started",[10,15,20,25,30,35],{"title":11,"path":12,"stem":13,"icon":14},"Learning Path","\u002Fgetting-started\u002Flearning-path","1.getting-started\u002F1.learning-path","i-lucide-map",{"title":16,"path":17,"stem":18,"icon":19},"Introduction","\u002Fgetting-started\u002Fintroduction","1.getting-started\u002F2.introduction","i-lucide-house",{"title":21,"path":22,"stem":23,"icon":24},"Installation","\u002Fgetting-started\u002Finstallation","1.getting-started\u002F3.installation","i-lucide-download",{"title":26,"path":27,"stem":28,"icon":29},"Your First Program","\u002Fgetting-started\u002Ffirst-program","1.getting-started\u002F4.first-program","i-lucide-play",{"title":31,"path":32,"stem":33,"icon":34},"Modules and Dependencies","\u002Fgetting-started\u002Fmodules-and-dependencies","1.getting-started\u002F5.modules-and-dependencies","i-lucide-package-open",{"title":36,"path":37,"stem":38,"icon":34},"Advanced Modules","\u002Fgetting-started\u002Fadvanced-modules","1.getting-started\u002F6.advanced-modules",{"title":40,"path":41,"stem":42,"children":43,"page":6},"Fundamentals","\u002Ffundamentals","2.fundamentals",[44,49,54,59,64,69,74,79,84,89,94,99,104,109],{"title":45,"path":46,"stem":47,"icon":48},"Program Structure","\u002Ffundamentals\u002Fprogram-structure","2.fundamentals\u002F1.program-structure","i-lucide-workflow",{"title":50,"path":51,"stem":52,"icon":53},"Structs and Methods","\u002Ffundamentals\u002Fstructs-and-methods","2.fundamentals\u002F10.structs-and-methods","i-lucide-box",{"title":55,"path":56,"stem":57,"icon":58},"Interfaces","\u002Ffundamentals\u002Finterfaces","2.fundamentals\u002F11.interfaces","i-lucide-plug",{"title":60,"path":61,"stem":62,"icon":63},"Error Handling","\u002Ffundamentals\u002Ferrors","2.fundamentals\u002F12.errors","i-lucide-triangle-alert",{"title":65,"path":66,"stem":67,"icon":68},"Defined Types and Type Aliases","\u002Ffundamentals\u002Fdefined-types","2.fundamentals\u002F13.defined-types","i-lucide-tags",{"title":70,"path":71,"stem":72,"icon":73},"Generics","\u002Ffundamentals\u002Fgenerics","2.fundamentals\u002F14.generics","i-lucide-blocks",{"title":75,"path":76,"stem":77,"icon":78},"Project Structure","\u002Ffundamentals\u002Fproject-structure","2.fundamentals\u002F2.project-structure","i-lucide-folder-tree",{"title":80,"path":81,"stem":82,"icon":83},"Project Layout","\u002Ffundamentals\u002Fproject-layout","2.fundamentals\u002F3.project-layout","i-lucide-layout",{"title":85,"path":86,"stem":87,"icon":88},"Variables and Data Types","\u002Ffundamentals\u002Fvariables-and-types","2.fundamentals\u002F4.variables-and-types","i-lucide-variable",{"title":90,"path":91,"stem":92,"icon":93},"Functions","\u002Ffundamentals\u002Ffunctions","2.fundamentals\u002F5.functions","i-lucide-braces",{"title":95,"path":96,"stem":97,"icon":98},"Control Statements","\u002Ffundamentals\u002Fcontrol-statements","2.fundamentals\u002F6.control-statements","i-lucide-git-branch",{"title":100,"path":101,"stem":102,"icon":103},"Pointers","\u002Ffundamentals\u002Fpointers","2.fundamentals\u002F7.pointers","i-lucide-mouse-pointer-2",{"title":105,"path":106,"stem":107,"icon":108},"Arrays and Slices","\u002Ffundamentals\u002Farrays-and-slices","2.fundamentals\u002F8.arrays-and-slices","i-lucide-list",{"title":110,"path":111,"stem":112,"icon":113},"Maps","\u002Ffundamentals\u002Fmaps","2.fundamentals\u002F9.maps","i-lucide-table-properties",{"title":115,"path":116,"stem":117,"children":118,"page":6},"Concurrency","\u002Fconcurrency","3.concurrency",[119,124,129,134,139],{"title":120,"path":121,"stem":122,"icon":123},"Concurrency Model","\u002Fconcurrency\u002Fconcurrency-model","3.concurrency\u002F1.concurrency-model","i-lucide-network",{"title":125,"path":126,"stem":127,"icon":128},"Goroutines","\u002Fconcurrency\u002Fgoroutines","3.concurrency\u002F2.goroutines","i-lucide-git-fork",{"title":130,"path":131,"stem":132,"icon":133},"Channels","\u002Fconcurrency\u002Fchannels","3.concurrency\u002F3.channels","i-lucide-arrow-left-right",{"title":135,"path":136,"stem":137,"icon":138},"Select and Cancellation","\u002Fconcurrency\u002Fselect-and-cancellation","3.concurrency\u002F4.select-and-cancellation","i-lucide-circle-stop",{"title":140,"path":141,"stem":142,"icon":143},"Synchronization","\u002Fconcurrency\u002Fsynchronization","3.concurrency\u002F5.synchronization","i-lucide-lock-keyhole",{"title":145,"path":146,"stem":147,"children":148,"page":6},"Standard Library","\u002Fstandard-library","4.standard-library",[149,154,159,164,169,174,179],{"title":150,"path":151,"stem":152,"icon":153},"Standard Library Overview","\u002Fstandard-library\u002Foverview","4.standard-library\u002F1.overview","i-lucide-library",{"title":155,"path":156,"stem":157,"icon":158},"Text and Formatting","\u002Fstandard-library\u002Ftext-and-formatting","4.standard-library\u002F2.text-and-formatting","i-lucide-text",{"title":160,"path":161,"stem":162,"icon":163},"Files and JSON","\u002Fstandard-library\u002Ffiles-and-json","4.standard-library\u002F3.files-and-json","i-lucide-file-json",{"title":165,"path":166,"stem":167,"icon":168},"HTTP Clients and Servers","\u002Fstandard-library\u002Fhttp","4.standard-library\u002F4.http","i-lucide-server",{"title":170,"path":171,"stem":172,"icon":173},"Testing","\u002Fstandard-library\u002Ftesting","4.standard-library\u002F5.testing","i-lucide-flask-conical",{"title":175,"path":176,"stem":177,"icon":178},"Command-Line Tools and Configuration","\u002Fstandard-library\u002Fcommand-line-and-configuration","4.standard-library\u002F6.command-line-and-configuration","i-lucide-terminal-square",{"title":180,"path":181,"stem":182,"icon":183},"Structured Logging","\u002Fstandard-library\u002Fstructured-logging","4.standard-library\u002F7.structured-logging","i-lucide-scroll-text",{"title":185,"path":186,"stem":187,"children":188,"page":6},"Backend Development","\u002Fbackend-development","5.backend-development",[189,194,199,204,209,214,219,224,229,234,239],{"title":190,"path":191,"stem":192,"icon":193},"Backend Roadmap","\u002Fbackend-development\u002Fbackend-roadmap","5.backend-development\u002F1.backend-roadmap","i-lucide-route",{"title":195,"path":196,"stem":197,"icon":198},"Graceful Shutdown","\u002Fbackend-development\u002Fgraceful-shutdown","5.backend-development\u002F10.graceful-shutdown","i-lucide-power",{"title":200,"path":201,"stem":202,"icon":203},"Production Checklist","\u002Fbackend-development\u002Fproduction-checklist","5.backend-development\u002F11.production-checklist","i-lucide-clipboard-check",{"title":205,"path":206,"stem":207,"icon":208},"HTTP API Design","\u002Fbackend-development\u002Fhttp-api-design","5.backend-development\u002F2.http-api-design","i-lucide-waypoints",{"title":210,"path":211,"stem":212,"icon":213},"Middleware","\u002Fbackend-development\u002Fmiddleware","5.backend-development\u002F3.middleware","i-lucide-layers-3",{"title":215,"path":216,"stem":217,"icon":218},"Validation","\u002Fbackend-development\u002Fvalidation","5.backend-development\u002F4.validation","i-lucide-list-checks",{"title":220,"path":221,"stem":222,"icon":223},"Application Wiring","\u002Fbackend-development\u002Fapplication-wiring","5.backend-development\u002F5.application-wiring","i-lucide-cable",{"title":225,"path":226,"stem":227,"icon":228},"Observability","\u002Fbackend-development\u002Fobservability","5.backend-development\u002F6.observability","i-lucide-activity",{"title":230,"path":231,"stem":232,"icon":233},"Database SQL","\u002Fbackend-development\u002Fdatabase-sql","5.backend-development\u002F7.database-sql","i-lucide-database",{"title":235,"path":236,"stem":237,"icon":238},"Transactions","\u002Fbackend-development\u002Ftransactions","5.backend-development\u002F8.transactions","i-lucide-repeat-2",{"title":240,"path":241,"stem":242,"icon":243},"Authentication and Authorization","\u002Fbackend-development\u002Fauthentication","5.backend-development\u002F9.authentication","i-lucide-shield-check",{"title":245,"path":246,"stem":247,"children":248,"page":6},"Advanced Go","\u002Fadvanced-go","6.advanced-go",[249,254,259,264,269,274,278,283,288],{"title":250,"path":251,"stem":252,"icon":253},"Advanced Go Roadmap","\u002Fadvanced-go\u002Fadvanced-roadmap","6.advanced-go\u002F1.advanced-roadmap","i-lucide-brain-circuit",{"title":255,"path":256,"stem":257,"icon":258},"Reflection","\u002Fadvanced-go\u002Freflection","6.advanced-go\u002F2.reflection","i-lucide-scan-search",{"title":260,"path":261,"stem":262,"icon":263},"Memory and Garbage Collection","\u002Fadvanced-go\u002Fmemory-and-garbage-collection","6.advanced-go\u002F3.memory-and-garbage-collection","i-lucide-memory-stick",{"title":265,"path":266,"stem":267,"icon":268},"Benchmarking and Profiling","\u002Fadvanced-go\u002Fbenchmarking-and-profiling","6.advanced-go\u002F4.benchmarking-and-profiling","i-lucide-gauge",{"title":270,"path":271,"stem":272,"icon":273},"Advanced Testing and Fuzzing","\u002Fadvanced-go\u002Fadvanced-testing-and-fuzzing","6.advanced-go\u002F5.advanced-testing-and-fuzzing","i-lucide-microscope",{"title":275,"path":276,"stem":277,"icon":48},"Advanced Concurrency","\u002Fadvanced-go\u002Fadvanced-concurrency","6.advanced-go\u002F6.advanced-concurrency",{"title":279,"path":280,"stem":281,"icon":282},"Unsafe and C Boundaries","\u002Fadvanced-go\u002Funsafe-and-cgo","6.advanced-go\u002F7.unsafe-and-cgo","i-lucide-radiation",{"title":284,"path":285,"stem":286,"icon":287},"Package and API Design","\u002Fadvanced-go\u002Fpackage-and-api-design","6.advanced-go\u002F8.package-and-api-design","i-lucide-boxes",{"title":289,"path":290,"stem":291,"icon":292},"Distributed Reliability","\u002Fadvanced-go\u002Fdistributed-reliability","6.advanced-go\u002F9.distributed-reliability","i-lucide-cloud-cog",{"title":294,"path":295,"stem":296,"children":297,"page":6},"Projects","\u002Fprojects","7.projects",[298,316,332,347],{"title":299,"path":300,"stem":301,"children":302,"icon":305},"CLI Task Manager","\u002Fprojects\u002Fcli-task-manager","7.projects\u002F1.cli-task-manager\u002Findex",[303,306,311],{"title":304,"path":300,"stem":301,"icon":305},"Project — CLI Task Manager","i-lucide-list-todo",{"title":307,"path":308,"stem":309,"icon":310},"CLI — Domain and Storage","\u002Fprojects\u002Fcli-task-manager\u002Fdomain-and-storage","7.projects\u002F1.cli-task-manager\u002F1.domain-and-storage","i-lucide-save",{"title":312,"path":313,"stem":314,"icon":315},"CLI — Commands and Tests","\u002Fprojects\u002Fcli-task-manager\u002Fcommands-and-tests","7.projects\u002F1.cli-task-manager\u002F2.commands-and-tests","i-lucide-terminal",{"title":317,"path":318,"stem":319,"children":320,"icon":323},"Concurrent URL Checker","\u002Fprojects\u002Fconcurrent-url-checker","7.projects\u002F2.concurrent-url-checker\u002Findex",[321,324,328],{"title":322,"path":318,"stem":319,"icon":323},"Project — Concurrent URL Checker","i-lucide-radar",{"title":325,"path":326,"stem":327,"icon":128},"URL Checker — Worker Pipeline","\u002Fprojects\u002Fconcurrent-url-checker\u002Fworker-pipeline","7.projects\u002F2.concurrent-url-checker\u002F1.worker-pipeline",{"title":329,"path":330,"stem":331,"icon":173},"URL Checker — Cancellation and Tests","\u002Fprojects\u002Fconcurrent-url-checker\u002Fcancellation-and-tests","7.projects\u002F2.concurrent-url-checker\u002F2.cancellation-and-tests",{"title":333,"path":334,"stem":335,"children":336},"File-backed API","\u002Fprojects\u002Ffile-backed-api","7.projects\u002F3.file-backed-api\u002Findex",[337,339,343],{"title":338,"path":334,"stem":335},"File-backed notes API",{"title":340,"path":341,"stem":342},"File store and concurrency","\u002Fprojects\u002Ffile-backed-api\u002Ffile-store-and-concurrency","7.projects\u002F3.file-backed-api\u002F1.file-store-and-concurrency",{"title":344,"path":345,"stem":346},"HTTP layer and tests","\u002Fprojects\u002Ffile-backed-api\u002Fhttp-and-tests","7.projects\u002F3.file-backed-api\u002F2.http-and-tests",{"title":348,"path":349,"stem":350,"children":351},"Production REST API","\u002Fprojects\u002Fproduction-rest-api","7.projects\u002F4.production-rest-api\u002Findex",[352,354,358],{"title":353,"path":349,"stem":350},"Production REST API capstone",{"title":355,"path":356,"stem":357},"Persistence and authentication","\u002Fprojects\u002Fproduction-rest-api\u002Fpersistence-and-auth","7.projects\u002F4.production-rest-api\u002F1.persistence-and-auth",{"title":359,"path":360,"stem":361},"Operations and release","\u002Fprojects\u002Fproduction-rest-api\u002Foperations-and-release","7.projects\u002F4.production-rest-api\u002F2.operations-and-release",{"title":363,"path":364,"stem":365,"children":366,"page":6},"Meta","\u002Fmeta","99.meta",[367,372],{"title":368,"path":369,"stem":370,"icon":371},"About","\u002Fmeta\u002Fabout","99.meta\u002F1.about","i-lucide-info",{"title":373,"path":374,"stem":375,"icon":376},"Acknowledgements","\u002Fmeta\u002Facknowledgements","99.meta\u002F2.acknowledgements","i-lucide-heart-handshake",{"id":378,"title":140,"body":379,"description":1191,"extension":1192,"links":1193,"meta":1194,"navigation":1195,"path":141,"seo":1196,"stem":142,"__hash__":1198},"docs\u002F3.concurrency\u002F5.synchronization.md",{"type":380,"value":381,"toc":1177},"minimark",[382,389,394,412,420,426,638,654,660,667,870,873,876,882,888,894,898,978,984,988,995,998,1002,1011,1015,1123,1126,1130,1136,1148,1152,1165,1168,1173],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Build clear understanding through explanation, examples, and practice.","Intermediate","Goroutines, channels, and pointers","45 minutes",[390,391,393],"h2",{"id":392},"what-you-will-learn","What you will learn",[395,396,397,406,409],"ul",{},[398,399,400,401,405],"li",{},"wait with ",[402,403,404],"code",{},"WaitGroup"," and protect invariants with mutexes;",[398,407,408],{},"choose between locks, atomics, and ownership;",[398,410,411],{},"build a bounded worker-pool shape;",[413,414,415,416,419],"p",{},"Channels are useful for communication. The ",[402,417,418],{},"sync"," package is useful when goroutines need to coordinate completion or safely share state.",[390,421,423,424],{"id":422},"wait-for-work-with-waitgroup","Wait for work with ",[402,425,404],{},[427,428,433],"pre",{"className":429,"code":430,"language":431,"meta":432,"style":432},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","var wg sync.WaitGroup\n\nfor i := 1; i \u003C= 3; i++ {\n    wg.Add(1)\n\n    go func(id int) {\n        defer wg.Done()\n        fmt.Println(\"worker\", id)\n    }(i)\n}\n\nwg.Wait()\n","go","",[402,434,435,459,466,506,527,532,556,572,603,614,620,625],{"__ignoreMap":432},[436,437,440,444,448,452,456],"span",{"class":438,"line":439},"line",1,[436,441,443],{"class":442},"sEOdo","var",[436,445,447],{"class":446},"s45-6"," wg",[436,449,451],{"class":450},"snwKR"," sync",[436,453,455],{"class":454},"shFME",".",[436,457,458],{"class":450},"WaitGroup\n",[436,460,462],{"class":438,"line":461},2,[436,463,465],{"emptyLinePlaceholder":464},true,"\n",[436,467,469,473,476,480,484,487,489,493,496,498,500,503],{"class":438,"line":468},3,[436,470,472],{"class":471},"s7Bcg","for",[436,474,475],{"class":446}," i",[436,477,479],{"class":478},"su2KV"," :=",[436,481,483],{"class":482},"sAJzr"," 1",[436,485,486],{"class":454},";",[436,488,475],{"class":446},[436,490,492],{"class":491},"sRBoR"," \u003C=",[436,494,495],{"class":482}," 3",[436,497,486],{"class":454},[436,499,475],{"class":446},[436,501,502],{"class":491},"++",[436,504,505],{"class":454}," {\n",[436,507,509,512,514,518,521,524],{"class":438,"line":508},4,[436,510,511],{"class":446},"    wg",[436,513,455],{"class":454},[436,515,517],{"class":516},"sisdC","Add",[436,519,520],{"class":454},"(",[436,522,523],{"class":482},"1",[436,525,526],{"class":454},")\n",[436,528,530],{"class":438,"line":529},5,[436,531,465],{"emptyLinePlaceholder":464},[436,533,535,538,541,543,547,551,554],{"class":438,"line":534},6,[436,536,537],{"class":471},"    go",[436,539,540],{"class":442}," func",[436,542,520],{"class":454},[436,544,546],{"class":545},"syre3","id",[436,548,550],{"class":549},"sF6sA"," int",[436,552,553],{"class":454},")",[436,555,505],{"class":454},[436,557,559,562,564,566,569],{"class":438,"line":558},7,[436,560,561],{"class":471},"        defer",[436,563,447],{"class":446},[436,565,455],{"class":454},[436,567,568],{"class":516},"Done",[436,570,571],{"class":454},"()\n",[436,573,575,578,580,583,585,589,593,595,598,601],{"class":438,"line":574},8,[436,576,577],{"class":446},"        fmt",[436,579,455],{"class":454},[436,581,582],{"class":516},"Println",[436,584,520],{"class":454},[436,586,588],{"class":587},"sED5a","\"",[436,590,592],{"class":591},"sVazx","worker",[436,594,588],{"class":587},[436,596,597],{"class":454},",",[436,599,600],{"class":446}," id",[436,602,526],{"class":454},[436,604,606,609,612],{"class":438,"line":605},9,[436,607,608],{"class":454},"    }(",[436,610,611],{"class":446},"i",[436,613,526],{"class":454},[436,615,617],{"class":438,"line":616},10,[436,618,619],{"class":454},"}\n",[436,621,623],{"class":438,"line":622},11,[436,624,465],{"emptyLinePlaceholder":464},[436,626,628,631,633,636],{"class":438,"line":627},12,[436,629,630],{"class":446},"wg",[436,632,455],{"class":454},[436,634,635],{"class":516},"Wait",[436,637,571],{"class":454},[413,639,640,641,643,644,646,647,650,651,653],{},"Call ",[402,642,517],{}," before starting the goroutine. Each worker calls ",[402,645,568],{},", normally with ",[402,648,649],{},"defer",", and ",[402,652,635],{}," blocks until the counter reaches zero.",[413,655,656,657,659],{},"A ",[402,658,404],{}," waits for completion; it does not collect errors or cancel sibling work.",[390,661,663,664],{"id":662},"protect-state-with-mutex","Protect state with ",[402,665,666],{},"Mutex",[427,668,670],{"className":429,"code":669,"language":431,"meta":432,"style":432},"type Counter struct {\n    mu    sync.Mutex\n    value int\n}\n\nfunc (c *Counter) Increment() {\n    c.mu.Lock()\n    defer c.mu.Unlock()\n    c.value++\n}\n\nfunc (c *Counter) Value() int {\n    c.mu.Lock()\n    defer c.mu.Unlock()\n    return c.value\n}\n",[402,671,672,685,698,706,710,714,741,758,777,789,793,797,820,835,852,865],{"__ignoreMap":432},[436,673,674,677,680,683],{"class":438,"line":439},[436,675,676],{"class":442},"type",[436,678,679],{"class":450}," Counter",[436,681,682],{"class":442}," struct",[436,684,505],{"class":454},[436,686,687,690,693,695],{"class":438,"line":461},[436,688,689],{"class":446},"    mu",[436,691,692],{"class":450},"    sync",[436,694,455],{"class":454},[436,696,697],{"class":450},"Mutex\n",[436,699,700,703],{"class":438,"line":468},[436,701,702],{"class":446},"    value",[436,704,705],{"class":549}," int\n",[436,707,708],{"class":438,"line":508},[436,709,619],{"class":454},[436,711,712],{"class":438,"line":529},[436,713,465],{"emptyLinePlaceholder":464},[436,715,716,719,722,725,728,731,733,736,739],{"class":438,"line":534},[436,717,718],{"class":442},"func",[436,720,721],{"class":454}," (",[436,723,724],{"class":545},"c ",[436,726,727],{"class":442},"*",[436,729,730],{"class":450},"Counter",[436,732,553],{"class":454},[436,734,735],{"class":516}," Increment",[436,737,738],{"class":454},"()",[436,740,505],{"class":454},[436,742,743,746,748,751,753,756],{"class":438,"line":558},[436,744,745],{"class":446},"    c",[436,747,455],{"class":454},[436,749,750],{"class":446},"mu",[436,752,455],{"class":454},[436,754,755],{"class":516},"Lock",[436,757,571],{"class":454},[436,759,760,763,766,768,770,772,775],{"class":438,"line":574},[436,761,762],{"class":471},"    defer",[436,764,765],{"class":446}," c",[436,767,455],{"class":454},[436,769,750],{"class":446},[436,771,455],{"class":454},[436,773,774],{"class":516},"Unlock",[436,776,571],{"class":454},[436,778,779,781,783,786],{"class":438,"line":605},[436,780,745],{"class":446},[436,782,455],{"class":454},[436,784,785],{"class":446},"value",[436,787,788],{"class":491},"++\n",[436,790,791],{"class":438,"line":616},[436,792,619],{"class":454},[436,794,795],{"class":438,"line":622},[436,796,465],{"emptyLinePlaceholder":464},[436,798,799,801,803,805,807,809,811,814,816,818],{"class":438,"line":627},[436,800,718],{"class":442},[436,802,721],{"class":454},[436,804,724],{"class":545},[436,806,727],{"class":442},[436,808,730],{"class":450},[436,810,553],{"class":454},[436,812,813],{"class":516}," Value",[436,815,738],{"class":454},[436,817,550],{"class":549},[436,819,505],{"class":454},[436,821,823,825,827,829,831,833],{"class":438,"line":822},13,[436,824,745],{"class":446},[436,826,455],{"class":454},[436,828,750],{"class":446},[436,830,455],{"class":454},[436,832,755],{"class":516},[436,834,571],{"class":454},[436,836,838,840,842,844,846,848,850],{"class":438,"line":837},14,[436,839,762],{"class":471},[436,841,765],{"class":446},[436,843,455],{"class":454},[436,845,750],{"class":446},[436,847,455],{"class":454},[436,849,774],{"class":516},[436,851,571],{"class":454},[436,853,855,858,860,862],{"class":438,"line":854},15,[436,856,857],{"class":471},"    return",[436,859,765],{"class":446},[436,861,455],{"class":454},[436,863,864],{"class":446},"value\n",[436,866,868],{"class":438,"line":867},16,[436,869,619],{"class":454},[413,871,872],{},"Every access to protected fields—including reads—must follow the same locking rule.",[413,874,875],{},"Keep critical sections short. Do not hold a lock while performing slow network calls, waiting on channels, or calling unknown code unless the design specifically requires it.",[390,877,879],{"id":878},"rwmutex",[402,880,881],{},"RWMutex",[413,883,884,887],{},[402,885,886],{},"sync.RWMutex"," allows several readers or one writer. It can help when reads are frequent, writes are rare, and the protected work is meaningful enough to justify the extra complexity.",[413,889,890,891,893],{},"Do not assume it is automatically faster than ",[402,892,666],{},". Measure before optimising.",[390,895,897],{"id":896},"run-initialization-once","Run initialization once",[427,899,901],{"className":429,"code":900,"language":431,"meta":432,"style":432},"var once sync.Once\n\nfunc initialize() {\n    once.Do(func() {\n        fmt.Println(\"runs once\")\n    })\n}\n",[402,902,903,917,921,932,950,969,974],{"__ignoreMap":432},[436,904,905,907,910,912,914],{"class":438,"line":439},[436,906,443],{"class":442},[436,908,909],{"class":446}," once",[436,911,451],{"class":450},[436,913,455],{"class":454},[436,915,916],{"class":450},"Once\n",[436,918,919],{"class":438,"line":461},[436,920,465],{"emptyLinePlaceholder":464},[436,922,923,925,928,930],{"class":438,"line":468},[436,924,718],{"class":442},[436,926,927],{"class":516}," initialize",[436,929,738],{"class":454},[436,931,505],{"class":454},[436,933,934,937,939,942,944,946,948],{"class":438,"line":508},[436,935,936],{"class":446},"    once",[436,938,455],{"class":454},[436,940,941],{"class":516},"Do",[436,943,520],{"class":454},[436,945,718],{"class":442},[436,947,738],{"class":454},[436,949,505],{"class":454},[436,951,952,954,956,958,960,962,965,967],{"class":438,"line":529},[436,953,577],{"class":446},[436,955,455],{"class":454},[436,957,582],{"class":516},[436,959,520],{"class":454},[436,961,588],{"class":587},[436,963,964],{"class":591},"runs once",[436,966,588],{"class":587},[436,968,526],{"class":454},[436,970,971],{"class":438,"line":534},[436,972,973],{"class":454},"    })\n",[436,975,976],{"class":438,"line":558},[436,977,619],{"class":454},[413,979,980,983],{},[402,981,982],{},"sync.Once"," is useful for one-time initialisation shared by goroutines. If the function panics, that call still counts as having returned; future calls do not run it again.",[390,985,987],{"id":986},"atomic-operations","Atomic operations",[413,989,990,991,994],{},"The ",[402,992,993],{},"sync\u002Fatomic"," package supports low-level operations on individual values. Atomics can be useful for counters or flags, but they do not automatically make a multi-step invariant safe.",[413,996,997],{},"Use a mutex when several fields or operations must change together. Clear code is more valuable than avoiding a small lock.",[390,999,1001],{"id":1000},"do-not-copy-synchronization-values","Do not copy synchronization values",[413,1003,1004,1005,1007,1008,1010],{},"Types such as ",[402,1006,666],{}," and ",[402,1009,404],{}," must not be copied after first use. Keep them as fields on a pointer-receiver struct and pass the struct by pointer when needed.",[390,1012,1014],{"id":1013},"a-worker-pool-shape","A worker-pool shape",[427,1016,1018],{"className":429,"code":1017,"language":431,"meta":432,"style":432},"func worker(jobs \u003C-chan int, results chan\u003C- int, wg *sync.WaitGroup) {\n    defer wg.Done()\n    for job := range jobs {\n        results \u003C- job * job\n    }\n}\n",[402,1019,1020,1070,1082,1100,1114,1119],{"__ignoreMap":432},[436,1021,1022,1024,1027,1029,1032,1035,1038,1040,1042,1045,1048,1051,1053,1055,1057,1060,1062,1064,1066,1068],{"class":438,"line":439},[436,1023,718],{"class":442},[436,1025,1026],{"class":516}," worker",[436,1028,520],{"class":454},[436,1030,1031],{"class":545},"jobs",[436,1033,1034],{"class":491}," \u003C-",[436,1036,1037],{"class":442},"chan",[436,1039,550],{"class":549},[436,1041,597],{"class":454},[436,1043,1044],{"class":545}," results",[436,1046,1047],{"class":442}," chan",[436,1049,1050],{"class":491},"\u003C-",[436,1052,550],{"class":549},[436,1054,597],{"class":454},[436,1056,447],{"class":545},[436,1058,1059],{"class":442}," *",[436,1061,418],{"class":450},[436,1063,455],{"class":454},[436,1065,404],{"class":450},[436,1067,553],{"class":454},[436,1069,505],{"class":454},[436,1071,1072,1074,1076,1078,1080],{"class":438,"line":461},[436,1073,762],{"class":471},[436,1075,447],{"class":446},[436,1077,455],{"class":454},[436,1079,568],{"class":516},[436,1081,571],{"class":454},[436,1083,1084,1087,1090,1092,1095,1098],{"class":438,"line":468},[436,1085,1086],{"class":471},"    for",[436,1088,1089],{"class":446}," job",[436,1091,479],{"class":478},[436,1093,1094],{"class":471}," range",[436,1096,1097],{"class":446}," jobs",[436,1099,505],{"class":454},[436,1101,1102,1105,1107,1109,1111],{"class":438,"line":508},[436,1103,1104],{"class":446},"        results",[436,1106,1034],{"class":491},[436,1108,1089],{"class":446},[436,1110,1059],{"class":442},[436,1112,1113],{"class":446}," job\n",[436,1115,1116],{"class":438,"line":529},[436,1117,1118],{"class":454},"    }\n",[436,1120,1121],{"class":438,"line":534},[436,1122,619],{"class":454},[413,1124,1125],{},"A complete worker pool also needs clear ownership for closing channels, error handling, cancellation, and a bounded number of workers.",[390,1127,1129],{"id":1128},"practice","Practice",[413,1131,1132,1133,455],{},"Build a safe in-memory counter used by 100 goroutines. Each goroutine should increment it 1,000 times. Wait for all work, print the result, and verify it is always ",[402,1134,1135],{},"100000",[1137,1138,1139],"accordion",{},[1140,1141,1144,1145,1147],"accordion-item",{"icon":1142,"label":1143},"i-lucide-lightbulb","Show solution","Use a fixed worker count, one jobs channel, and a ",[402,1146,404],{},". Protect truly shared composite state with one mutex covering its complete invariant. Close results only after workers finish; atomics are appropriate only for simple independent values.",[390,1149,1151],{"id":1150},"summary","Summary",[395,1153,1154,1159,1162],{},[398,1155,1156,1158],{},[402,1157,404],{}," waits; it does not protect data.",[398,1160,1161],{},"A mutex must cover the entire shared invariant, not isolated field accesses.",[398,1163,1164],{},"Bound worker counts and queues to bound resource use.",[1166,1167],"learning-lesson-complete",{},[1169,1170],"learning-lesson-navigation",{"next":151,"nextTitle":1171,"previous":136,"previousTitle":1172},"Standard library overview","Select and cancellation",[1174,1175,1176],"style",{},"html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}html pre.shiki code .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}html pre.shiki code .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}html pre.shiki code .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .s7Bcg, html code.shiki .s7Bcg{--shiki-light:#859900;--shiki-light-font-style:inherit;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#C678DD;--shiki-dark-font-style:inherit}html pre.shiki code .su2KV, html code.shiki .su2KV{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#E5C07B}html pre.shiki code .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}html pre.shiki code .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}html pre.shiki code .syre3, html code.shiki .syre3{--shiki-light:#657B83;--shiki-light-font-style:inherit;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#E06C75;--shiki-dark-font-style:italic}html pre.shiki code .sF6sA, html code.shiki .sF6sA{--shiki-light:#586E75;--shiki-light-font-weight:bold;--shiki-default:#C792EA;--shiki-default-font-weight:inherit;--shiki-dark:#C678DD;--shiki-dark-font-weight:inherit}html pre.shiki code .sED5a, html code.shiki .sED5a{--shiki-light:#2AA198;--shiki-default:#89DDFF;--shiki-dark:#98C379}html pre.shiki code .sVazx, html code.shiki .sVazx{--shiki-light:#2AA198;--shiki-default:#C3E88D;--shiki-dark:#98C379}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":432,"searchDepth":461,"depth":461,"links":1178},[1179,1180,1182,1184,1185,1186,1187,1188,1189,1190],{"id":392,"depth":461,"text":393},{"id":422,"depth":461,"text":1181},"Wait for work with WaitGroup",{"id":662,"depth":461,"text":1183},"Protect state with Mutex",{"id":878,"depth":461,"text":881},{"id":896,"depth":461,"text":897},{"id":986,"depth":461,"text":987},{"id":1000,"depth":461,"text":1001},{"id":1013,"depth":461,"text":1014},{"id":1128,"depth":461,"text":1129},{"id":1150,"depth":461,"text":1151},"Coordinate goroutines safely with WaitGroup, Mutex, RWMutex, Once, and atomic operations.","md",null,{},{"icon":143},{"description":1197,"title":140},"Learn when and how to use sync.WaitGroup, Mutex, RWMutex, Once, and atomic operations in Go.","skMH5vCFe-Kjbrpgyvyy23lr9KeMDBwDO39LH-0BigU",[1200,1202],{"title":135,"path":136,"stem":137,"description":1201,"icon":138,"children":-1},"Wait on multiple channel operations and stop concurrent work with context.",{"title":150,"path":151,"stem":152,"description":1203,"icon":153,"children":-1},"Learn how to discover and combine Go's built-in packages to solve real problems.",1794118099933]