[{"data":1,"prerenderedAt":1207},["ShallowReactive",2],{"navigation_docs":3,"-concurrency-select-and-cancellation":377,"-concurrency-select-and-cancellation-surround":1202},[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":135,"body":379,"description":1194,"extension":1195,"links":1196,"meta":1197,"navigation":1198,"path":136,"seo":1199,"stem":137,"__hash__":1201},"docs\u002F3.concurrency\u002F4.select-and-cancellation.md",{"type":380,"value":381,"toc":1184},"minimark",[382,388,393,411,417,543,549,553,652,663,667,674,742,748,752,755,762,952,958,962,1094,1097,1101,1127,1131,1138,1157,1161,1174,1177,1180],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":130,"time":387},"Build clear understanding through explanation, examples, and practice.","Intermediate","40 minutes",[389,390,392],"h2",{"id":391},"what-you-will-learn","What you will learn",[394,395,396,405,408],"ul",{},[397,398,399,400,404],"li",{},"coordinate multiple channel operations with ",[401,402,403],"code",{},"select",";",[397,406,407],{},"propagate cancellation using contexts;",[397,409,410],{},"avoid leaked timeout and background work;",[412,413,414,416],"p",{},[401,415,403],{}," waits for one of several channel operations to become ready. It is the control statement that connects concurrent events.",[418,419,424],"pre",{"className":420,"code":421,"language":422,"meta":423,"style":423},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","select {\ncase value := \u003C-results:\n    fmt.Println(\"result:\", value)\ncase err := \u003C-failures:\n    fmt.Println(\"failed:\", err)\n}\n","go","",[401,425,426,438,462,496,513,537],{"__ignoreMap":423},[427,428,431,434],"span",{"class":429,"line":430},"line",1,[427,432,403],{"class":433},"s7Bcg",[427,435,437],{"class":436},"shFME"," {\n",[427,439,441,444,448,452,456,459],{"class":429,"line":440},2,[427,442,443],{"class":433},"case",[427,445,447],{"class":446},"s45-6"," value",[427,449,451],{"class":450},"su2KV"," :=",[427,453,455],{"class":454},"sRBoR"," \u003C-",[427,457,458],{"class":446},"results",[427,460,461],{"class":436},":\n",[427,463,465,468,471,475,478,482,486,488,491,493],{"class":429,"line":464},3,[427,466,467],{"class":446},"    fmt",[427,469,470],{"class":436},".",[427,472,474],{"class":473},"sisdC","Println",[427,476,477],{"class":436},"(",[427,479,481],{"class":480},"sED5a","\"",[427,483,485],{"class":484},"sVazx","result:",[427,487,481],{"class":480},[427,489,490],{"class":436},",",[427,492,447],{"class":446},[427,494,495],{"class":436},")\n",[427,497,499,501,504,506,508,511],{"class":429,"line":498},4,[427,500,443],{"class":433},[427,502,503],{"class":446}," err",[427,505,451],{"class":450},[427,507,455],{"class":454},[427,509,510],{"class":446},"failures",[427,512,461],{"class":436},[427,514,516,518,520,522,524,526,529,531,533,535],{"class":429,"line":515},5,[427,517,467],{"class":446},[427,519,470],{"class":436},[427,521,474],{"class":473},[427,523,477],{"class":436},[427,525,481],{"class":480},[427,527,528],{"class":484},"failed:",[427,530,481],{"class":480},[427,532,490],{"class":436},[427,534,503],{"class":446},[427,536,495],{"class":436},[427,538,540],{"class":429,"line":539},6,[427,541,542],{"class":436},"}\n",[412,544,545,546,548],{},"If several cases are ready, Go chooses one pseudo-randomly. If none is ready, ",[401,547,403],{}," blocks.",[389,550,552],{"id":551},"timeouts","Timeouts",[418,554,556],{"className":420,"code":555,"language":422,"meta":423,"style":423},"select {\ncase result := \u003C-results:\n    fmt.Println(result)\ncase \u003C-time.After(2 * time.Second):\n    fmt.Println(\"timed out\")\n}\n",[401,557,558,564,579,594,629,648],{"__ignoreMap":423},[427,559,560,562],{"class":429,"line":430},[427,561,403],{"class":433},[427,563,437],{"class":436},[427,565,566,568,571,573,575,577],{"class":429,"line":440},[427,567,443],{"class":433},[427,569,570],{"class":446}," result",[427,572,451],{"class":450},[427,574,455],{"class":454},[427,576,458],{"class":446},[427,578,461],{"class":436},[427,580,581,583,585,587,589,592],{"class":429,"line":464},[427,582,467],{"class":446},[427,584,470],{"class":436},[427,586,474],{"class":473},[427,588,477],{"class":436},[427,590,591],{"class":446},"result",[427,593,495],{"class":436},[427,595,596,598,600,603,605,608,610,614,618,621,623,626],{"class":429,"line":498},[427,597,443],{"class":433},[427,599,455],{"class":454},[427,601,602],{"class":446},"time",[427,604,470],{"class":436},[427,606,607],{"class":473},"After",[427,609,477],{"class":436},[427,611,613],{"class":612},"sAJzr","2",[427,615,617],{"class":616},"sEOdo"," *",[427,619,620],{"class":446}," time",[427,622,470],{"class":436},[427,624,625],{"class":446},"Second",[427,627,628],{"class":436},"):\n",[427,630,631,633,635,637,639,641,644,646],{"class":429,"line":515},[427,632,467],{"class":446},[427,634,470],{"class":436},[427,636,474],{"class":473},[427,638,477],{"class":436},[427,640,481],{"class":480},[427,642,643],{"class":484},"timed out",[427,645,481],{"class":480},[427,647,495],{"class":436},[427,649,650],{"class":429,"line":539},[427,651,542],{"class":436},[412,653,654,655,658,659,662],{},"This is convenient for one-off waits. In repeated loops, use and stop a ",[401,656,657],{},"time.Timer"," instead of repeatedly allocating ",[401,660,661],{},"time.After"," timers.",[389,664,666],{"id":665},"non-blocking-operations","Non-blocking operations",[412,668,669,670,673],{},"A ",[401,671,672],{},"default"," case runs immediately when no channel case is ready:",[418,675,677],{"className":420,"code":676,"language":422,"meta":423,"style":423},"select {\ncase job := \u003C-jobs:\n    process(job)\ndefault:\n    fmt.Println(\"no job available\")\n}\n",[401,678,679,685,701,713,719,738],{"__ignoreMap":423},[427,680,681,683],{"class":429,"line":430},[427,682,403],{"class":433},[427,684,437],{"class":436},[427,686,687,689,692,694,696,699],{"class":429,"line":440},[427,688,443],{"class":433},[427,690,691],{"class":446}," job",[427,693,451],{"class":450},[427,695,455],{"class":454},[427,697,698],{"class":446},"jobs",[427,700,461],{"class":436},[427,702,703,706,708,711],{"class":429,"line":464},[427,704,705],{"class":473},"    process",[427,707,477],{"class":436},[427,709,710],{"class":446},"job",[427,712,495],{"class":436},[427,714,715,717],{"class":429,"line":498},[427,716,672],{"class":433},[427,718,461],{"class":436},[427,720,721,723,725,727,729,731,734,736],{"class":429,"line":515},[427,722,467],{"class":446},[427,724,470],{"class":436},[427,726,474],{"class":473},[427,728,477],{"class":436},[427,730,481],{"class":480},[427,732,733],{"class":484},"no job available",[427,735,481],{"class":480},[427,737,495],{"class":436},[427,739,740],{"class":429,"line":539},[427,741,542],{"class":436},[412,743,744,745,747],{},"Use this carefully. A loop containing a ",[401,746,672],{}," case can spin continuously and waste CPU.",[389,749,751],{"id":750},"why-cancellation-matters","Why cancellation matters",[412,753,754],{},"Suppose an HTTP request starts several goroutines. If the client disconnects, those goroutines should stop instead of continuing work nobody needs.",[412,756,757,758,761],{},"The ",[401,759,760],{},"context"," package carries cancellation, deadlines, and request-scoped values across API boundaries.",[418,763,765],{"className":420,"code":764,"language":422,"meta":423,"style":423},"func work(ctx context.Context, items \u003C-chan string) error {\n    for {\n        select {\n        case \u003C-ctx.Done():\n            return ctx.Err()\n        case item, ok := \u003C-items:\n            if !ok {\n                return nil\n            }\n            fmt.Println(\"processing\", item)\n        }\n    }\n}\n",[401,766,767,812,819,826,843,859,880,894,904,910,935,941,947],{"__ignoreMap":423},[427,768,769,772,775,777,781,785,787,790,792,795,797,800,804,807,810],{"class":429,"line":430},[427,770,771],{"class":616},"func",[427,773,774],{"class":473}," work",[427,776,477],{"class":436},[427,778,780],{"class":779},"syre3","ctx",[427,782,784],{"class":783},"snwKR"," context",[427,786,470],{"class":436},[427,788,789],{"class":783},"Context",[427,791,490],{"class":436},[427,793,794],{"class":779}," items",[427,796,455],{"class":454},[427,798,799],{"class":616},"chan",[427,801,803],{"class":802},"sF6sA"," string",[427,805,806],{"class":436},")",[427,808,809],{"class":802}," error",[427,811,437],{"class":436},[427,813,814,817],{"class":429,"line":440},[427,815,816],{"class":433},"    for",[427,818,437],{"class":436},[427,820,821,824],{"class":429,"line":464},[427,822,823],{"class":433},"        select",[427,825,437],{"class":436},[427,827,828,831,833,835,837,840],{"class":429,"line":498},[427,829,830],{"class":433},"        case",[427,832,455],{"class":454},[427,834,780],{"class":446},[427,836,470],{"class":436},[427,838,839],{"class":473},"Done",[427,841,842],{"class":436},"():\n",[427,844,845,848,851,853,856],{"class":429,"line":515},[427,846,847],{"class":433},"            return",[427,849,850],{"class":446}," ctx",[427,852,470],{"class":436},[427,854,855],{"class":473},"Err",[427,857,858],{"class":436},"()\n",[427,860,861,863,866,868,871,873,875,878],{"class":429,"line":539},[427,862,830],{"class":433},[427,864,865],{"class":446}," item",[427,867,490],{"class":436},[427,869,870],{"class":446}," ok",[427,872,451],{"class":450},[427,874,455],{"class":454},[427,876,877],{"class":446},"items",[427,879,461],{"class":436},[427,881,883,886,889,892],{"class":429,"line":882},7,[427,884,885],{"class":433},"            if",[427,887,888],{"class":454}," !",[427,890,891],{"class":446},"ok",[427,893,437],{"class":436},[427,895,897,900],{"class":429,"line":896},8,[427,898,899],{"class":433},"                return",[427,901,903],{"class":902},"sp8Z5"," nil\n",[427,905,907],{"class":429,"line":906},9,[427,908,909],{"class":436},"            }\n",[427,911,913,916,918,920,922,924,927,929,931,933],{"class":429,"line":912},10,[427,914,915],{"class":446},"            fmt",[427,917,470],{"class":436},[427,919,474],{"class":473},[427,921,477],{"class":436},[427,923,481],{"class":480},[427,925,926],{"class":484},"processing",[427,928,481],{"class":480},[427,930,490],{"class":436},[427,932,865],{"class":446},[427,934,495],{"class":436},[427,936,938],{"class":429,"line":937},11,[427,939,940],{"class":436},"        }\n",[427,942,944],{"class":429,"line":943},12,[427,945,946],{"class":436},"    }\n",[427,948,950],{"class":429,"line":949},13,[427,951,542],{"class":436},[412,953,954,957],{},[401,955,956],{},"ctx.Done()"," is closed when the context is cancelled or reaches its deadline.",[389,959,961],{"id":960},"create-and-cancel-a-context","Create and cancel a context",[418,963,965],{"className":420,"code":964,"language":422,"meta":423,"style":423},"ctx, cancel := context.WithCancel(context.Background())\ndefer cancel()\n\ngo func() {\n    if err := work(ctx); err != nil {\n        log.Println(err)\n    }\n}()\n\n\u002F\u002F Later, when the work is no longer needed:\ncancel()\n",[401,966,967,997,1006,1012,1024,1052,1068,1072,1077,1081,1087],{"__ignoreMap":423},[427,968,969,971,973,976,978,980,982,985,987,989,991,994],{"class":429,"line":430},[427,970,780],{"class":446},[427,972,490],{"class":436},[427,974,975],{"class":446}," cancel",[427,977,451],{"class":450},[427,979,784],{"class":446},[427,981,470],{"class":436},[427,983,984],{"class":473},"WithCancel",[427,986,477],{"class":436},[427,988,760],{"class":446},[427,990,470],{"class":436},[427,992,993],{"class":473},"Background",[427,995,996],{"class":436},"())\n",[427,998,999,1002,1004],{"class":429,"line":440},[427,1000,1001],{"class":433},"defer",[427,1003,975],{"class":473},[427,1005,858],{"class":436},[427,1007,1008],{"class":429,"line":464},[427,1009,1011],{"emptyLinePlaceholder":1010},true,"\n",[427,1013,1014,1016,1019,1022],{"class":429,"line":498},[427,1015,422],{"class":433},[427,1017,1018],{"class":616}," func",[427,1020,1021],{"class":436},"()",[427,1023,437],{"class":436},[427,1025,1026,1029,1031,1033,1035,1037,1039,1042,1044,1047,1050],{"class":429,"line":515},[427,1027,1028],{"class":433},"    if",[427,1030,503],{"class":446},[427,1032,451],{"class":450},[427,1034,774],{"class":473},[427,1036,477],{"class":436},[427,1038,780],{"class":446},[427,1040,1041],{"class":436},");",[427,1043,503],{"class":446},[427,1045,1046],{"class":454}," !=",[427,1048,1049],{"class":902}," nil",[427,1051,437],{"class":436},[427,1053,1054,1057,1059,1061,1063,1066],{"class":429,"line":539},[427,1055,1056],{"class":446},"        log",[427,1058,470],{"class":436},[427,1060,474],{"class":473},[427,1062,477],{"class":436},[427,1064,1065],{"class":446},"err",[427,1067,495],{"class":436},[427,1069,1070],{"class":429,"line":882},[427,1071,946],{"class":436},[427,1073,1074],{"class":429,"line":896},[427,1075,1076],{"class":436},"}()\n",[427,1078,1079],{"class":429,"line":906},[427,1080,1011],{"emptyLinePlaceholder":1010},[427,1082,1083],{"class":429,"line":912},[427,1084,1086],{"class":1085},"sJsHy","\u002F\u002F Later, when the work is no longer needed:\n",[427,1088,1089,1092],{"class":429,"line":937},[427,1090,1091],{"class":473},"cancel",[427,1093,858],{"class":436},[412,1095,1096],{},"Always call the returned cancel function. It releases resources even if the operation finishes before the deadline.",[389,1098,1100],{"id":1099},"context-rules","Context rules",[394,1102,1103,1112,1115,1121,1124],{},[397,1104,1105,1106,1109,1110,470],{},"Accept ",[401,1107,1108],{},"context.Context"," as the first parameter, usually named ",[401,1111,780],{},[397,1113,1114],{},"Do not store a context in a struct for ordinary request work.",[397,1116,1117,1118,470],{},"Pass the existing context down instead of replacing it with ",[401,1119,1120],{},"context.Background()",[397,1122,1123],{},"Use context values only for request-scoped metadata, not ordinary optional parameters.",[397,1125,1126],{},"A function that starts work should document whether cancellation stops that work promptly.",[389,1128,1130],{"id":1129},"practice","Practice",[412,1132,1133,1134,1137],{},"Create a worker that prints a counter every 100 milliseconds. Stop it through a context after one second, and make ",[401,1135,1136],{},"main"," wait for the worker to confirm it has returned.",[1139,1140,1141],"accordion",{},[1142,1143,1146,1147,1149,1150,1152,1153,1156],"accordion-item",{"icon":1144,"label":1145},"i-lucide-lightbulb","Show solution","Accept a ",[401,1148,1108],{}," as the first parameter, select on both the work operation and ",[401,1151,956],{},", and return ",[401,1154,1155],{},"ctx.Err()"," when cancelled. The caller creates and cancels the context; do not store it in a long-lived struct.",[389,1158,1160],{"id":1159},"summary","Summary",[394,1162,1163,1168,1171],{},[397,1164,1165,1167],{},[401,1166,403],{}," waits on whichever channel case can proceed.",[397,1169,1170],{},"Context propagates deadlines and cancellation across call boundaries.",[397,1172,1173],{},"Cancellation must be observed by every blocking stage to stop the full pipeline.",[1175,1176],"learning-lesson-complete",{},[1178,1179],"learning-lesson-navigation",{"next":141,"nextTitle":140,"previous":131,"previousTitle":130},[1181,1182,1183],"style",{},"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 .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}html pre.shiki code .su2KV, html code.shiki .su2KV{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#E5C07B}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 .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);}html pre.shiki code .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}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 .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}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 .sp8Z5, html code.shiki .sp8Z5{--shiki-light:#B58900;--shiki-default:#89DDFF;--shiki-dark:#D19A66}html pre.shiki code .sJsHy, html code.shiki .sJsHy{--shiki-light:#93A1A1;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic}",{"title":423,"searchDepth":440,"depth":440,"links":1185},[1186,1187,1188,1189,1190,1191,1192,1193],{"id":391,"depth":440,"text":392},{"id":551,"depth":440,"text":552},{"id":665,"depth":440,"text":666},{"id":750,"depth":440,"text":751},{"id":960,"depth":440,"text":961},{"id":1099,"depth":440,"text":1100},{"id":1129,"depth":440,"text":1130},{"id":1159,"depth":440,"text":1160},"Wait on multiple channel operations and stop concurrent work with context.","md",null,{},{"icon":138},{"description":1200,"title":135},"Learn Go select statements, timeouts, context cancellation, deadlines, and clean goroutine shutdown.","eeIZJxFQBSoT2qB6X8TjRAcZoo3yATTCIPvItxHiob4",[1203,1205],{"title":130,"path":131,"stem":132,"description":1204,"icon":133,"children":-1},"Send values between goroutines and understand blocking, buffering, closing, and ownership.",{"title":140,"path":141,"stem":142,"description":1206,"icon":143,"children":-1},"Coordinate goroutines safely with WaitGroup, Mutex, RWMutex, Once, and atomic operations.",1794118099755]