[{"data":1,"prerenderedAt":1020},["ShallowReactive",2],{"navigation_docs":3,"-concurrency-channels":377,"-concurrency-channels-surround":1015},[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":130,"body":379,"description":1007,"extension":1008,"links":1009,"meta":1010,"navigation":1011,"path":131,"seo":1012,"stem":132,"__hash__":1014},"docs\u002F3.concurrency\u002F3.channels.md",{"type":380,"value":381,"toc":995},"minimark",[382,388,393,397,400,442,449,453,496,499,610,614,669,672,675,679,682,698,701,704,708,750,753,756,813,817,820,911,921,925,928,932,935,939,942,955,959,962,984,987,991],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":125,"time":387},"Build clear understanding through explanation, examples, and practice.","Intermediate","35 minutes",[389,390,392],"h2",{"id":391},"what-you-will-learn","What you will learn",[394,395,396],"p",{},"This lesson explains three related decisions: how a send meets a receive, when\nbounded buffering solves a real coordination problem, and which part of a\nprogram may safely announce that no further values will arrive.",[394,398,399],{},"A channel is a typed connection used by goroutines to send and receive values.\nUnlike a plain variable, it gives the two goroutines a defined rendezvous point:\nthe sender cannot treat the hand-off as complete until the channel's buffering\nrules allow it, and the receiver cannot invent a value before one is available.",[401,402,407],"pre",{"className":403,"code":404,"language":405,"meta":406,"style":406},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","messages := make(chan string)\n","go","",[408,409,410],"code",{"__ignoreMap":406},[411,412,415,419,423,427,431,435,439],"span",{"class":413,"line":414},"line",1,[411,416,418],{"class":417},"s45-6","messages",[411,420,422],{"class":421},"su2KV"," :=",[411,424,426],{"class":425},"sisdC"," make",[411,428,430],{"class":429},"shFME","(",[411,432,434],{"class":433},"sEOdo","chan",[411,436,438],{"class":437},"sF6sA"," string",[411,440,441],{"class":429},")\n",[394,443,444,445,448],{},"Its type is ",[408,446,447],{},"chan string",": a channel carrying strings.",[389,450,452],{"id":451},"send-and-receive","Send and receive",[401,454,456],{"className":403,"code":455,"language":405,"meta":406,"style":406},"messages \u003C- \"ready\" \u002F\u002F send\nmessage := \u003C-messages \u002F\u002F receive\n",[408,457,458,481],{"__ignoreMap":406},[411,459,460,462,466,470,474,477],{"class":413,"line":414},[411,461,418],{"class":417},[411,463,465],{"class":464},"sRBoR"," \u003C-",[411,467,469],{"class":468},"sED5a"," \"",[411,471,473],{"class":472},"sVazx","ready",[411,475,476],{"class":468},"\"",[411,478,480],{"class":479},"sJsHy"," \u002F\u002F send\n",[411,482,484,487,489,491,493],{"class":413,"line":483},2,[411,485,486],{"class":417},"message",[411,488,422],{"class":421},[411,490,465],{"class":464},[411,492,418],{"class":417},[411,494,495],{"class":479}," \u002F\u002F receive\n",[394,497,498],{},"For an unbuffered channel, a send waits until another goroutine is ready to receive, and a receive waits until another goroutine is ready to send. This transfers both a value and a synchronisation signal.",[401,500,502],{"className":403,"code":501,"language":405,"meta":406,"style":406},"func main() {\n    messages := make(chan string)\n\n    go func() {\n        messages \u003C- \"work complete\"\n    }()\n\n    fmt.Println(\u003C-messages)\n}\n",[408,503,504,518,535,542,556,572,578,583,604],{"__ignoreMap":406},[411,505,506,509,512,515],{"class":413,"line":414},[411,507,508],{"class":433},"func",[411,510,511],{"class":425}," main",[411,513,514],{"class":429},"()",[411,516,517],{"class":429}," {\n",[411,519,520,523,525,527,529,531,533],{"class":413,"line":483},[411,521,522],{"class":417},"    messages",[411,524,422],{"class":421},[411,526,426],{"class":425},[411,528,430],{"class":429},[411,530,434],{"class":433},[411,532,438],{"class":437},[411,534,441],{"class":429},[411,536,538],{"class":413,"line":537},3,[411,539,541],{"emptyLinePlaceholder":540},true,"\n",[411,543,545,549,552,554],{"class":413,"line":544},4,[411,546,548],{"class":547},"s7Bcg","    go",[411,550,551],{"class":433}," func",[411,553,514],{"class":429},[411,555,517],{"class":429},[411,557,559,562,564,566,569],{"class":413,"line":558},5,[411,560,561],{"class":417},"        messages",[411,563,465],{"class":464},[411,565,469],{"class":468},[411,567,568],{"class":472},"work complete",[411,570,571],{"class":468},"\"\n",[411,573,575],{"class":413,"line":574},6,[411,576,577],{"class":429},"    }()\n",[411,579,581],{"class":413,"line":580},7,[411,582,541],{"emptyLinePlaceholder":540},[411,584,586,589,592,595,597,600,602],{"class":413,"line":585},8,[411,587,588],{"class":417},"    fmt",[411,590,591],{"class":429},".",[411,593,594],{"class":425},"Println",[411,596,430],{"class":429},[411,598,599],{"class":464},"\u003C-",[411,601,418],{"class":417},[411,603,441],{"class":429},[411,605,607],{"class":413,"line":606},9,[411,608,609],{"class":429},"}\n",[389,611,613],{"id":612},"buffered-channels","Buffered channels",[401,615,617],{"className":403,"code":616,"language":405,"meta":406,"style":406},"queue := make(chan string, 2)\nqueue \u003C- \"first\"\nqueue \u003C- \"second\"\n",[408,618,619,643,656],{"__ignoreMap":406},[411,620,621,624,626,628,630,632,634,637,641],{"class":413,"line":414},[411,622,623],{"class":417},"queue",[411,625,422],{"class":421},[411,627,426],{"class":425},[411,629,430],{"class":429},[411,631,434],{"class":433},[411,633,438],{"class":437},[411,635,636],{"class":429},",",[411,638,640],{"class":639},"sAJzr"," 2",[411,642,441],{"class":429},[411,644,645,647,649,651,654],{"class":413,"line":483},[411,646,623],{"class":417},[411,648,465],{"class":464},[411,650,469],{"class":468},[411,652,653],{"class":472},"first",[411,655,571],{"class":468},[411,657,658,660,662,664,667],{"class":413,"line":537},[411,659,623],{"class":417},[411,661,465],{"class":464},[411,663,469],{"class":468},[411,665,666],{"class":472},"second",[411,668,571],{"class":468},[394,670,671],{},"A buffered channel lets sends proceed until the buffer is full. A receive waits\nonly when the buffer is empty. The capacity therefore specifies how much work\nmay be outstanding at this boundary; it is not a performance setting to choose\nby habit.",[394,673,674],{},"Buffers can absorb short bursts and limit the number of queued items. They do not remove the need to design backpressure or cancellation.",[389,676,678],{"id":677},"closing-a-channel","Closing a channel",[394,680,681],{},"Closing means no more values will be sent.",[401,683,685],{"className":403,"code":684,"language":405,"meta":406,"style":406},"close(queue)\n",[408,686,687],{"__ignoreMap":406},[411,688,689,692,694,696],{"class":413,"line":414},[411,690,691],{"class":425},"close",[411,693,430],{"class":429},[411,695,623],{"class":417},[411,697,441],{"class":429},[394,699,700],{},"The component that can prove that no more sends will happen should close the\nchannel. That is often the sending side or a coordinator that waits for all\nsenders. A receiver normally cannot make that proof: another sender may still\nbe about to write.",[394,702,703],{},"Sending to a closed channel panics. Closing an already-closed channel also panics.",[389,705,707],{"id":706},"receive-until-closed","Receive until closed",[401,709,711],{"className":403,"code":710,"language":405,"meta":406,"style":406},"for value := range queue {\n    fmt.Println(value)\n}\n",[408,712,713,731,746],{"__ignoreMap":406},[411,714,715,718,721,723,726,729],{"class":413,"line":414},[411,716,717],{"class":547},"for",[411,719,720],{"class":417}," value",[411,722,422],{"class":421},[411,724,725],{"class":547}," range",[411,727,728],{"class":417}," queue",[411,730,517],{"class":429},[411,732,733,735,737,739,741,744],{"class":413,"line":483},[411,734,588],{"class":417},[411,736,591],{"class":429},[411,738,594],{"class":425},[411,740,430],{"class":429},[411,742,743],{"class":417},"value",[411,745,441],{"class":429},[411,747,748],{"class":413,"line":537},[411,749,609],{"class":429},[394,751,752],{},"The loop ends after the channel is closed and all buffered values have been received.",[394,754,755],{},"You can inspect closure directly:",[401,757,759],{"className":403,"code":758,"language":405,"meta":406,"style":406},"value, ok := \u003C-queue\nif !ok {\n    fmt.Println(\"channel is closed\")\n}\n",[408,760,761,777,790,809],{"__ignoreMap":406},[411,762,763,765,767,770,772,774],{"class":413,"line":414},[411,764,743],{"class":417},[411,766,636],{"class":429},[411,768,769],{"class":417}," ok",[411,771,422],{"class":421},[411,773,465],{"class":464},[411,775,776],{"class":417},"queue\n",[411,778,779,782,785,788],{"class":413,"line":483},[411,780,781],{"class":547},"if",[411,783,784],{"class":464}," !",[411,786,787],{"class":417},"ok",[411,789,517],{"class":429},[411,791,792,794,796,798,800,802,805,807],{"class":413,"line":537},[411,793,588],{"class":417},[411,795,591],{"class":429},[411,797,594],{"class":425},[411,799,430],{"class":429},[411,801,476],{"class":468},[411,803,804],{"class":472},"channel is closed",[411,806,476],{"class":468},[411,808,441],{"class":429},[411,810,811],{"class":413,"line":544},[411,812,609],{"class":429},[389,814,816],{"id":815},"directional-channel-types","Directional channel types",[394,818,819],{},"Function signatures can communicate ownership:",[401,821,823],{"className":403,"code":822,"language":405,"meta":406,"style":406},"func produce(out chan\u003C- int) {\n    out \u003C- 42\n}\n\nfunc consume(in \u003C-chan int) {\n    fmt.Println(\u003C-in)\n}\n",[408,824,825,851,861,865,869,891,907],{"__ignoreMap":406},[411,826,827,829,832,834,838,841,843,846,849],{"class":413,"line":414},[411,828,508],{"class":433},[411,830,831],{"class":425}," produce",[411,833,430],{"class":429},[411,835,837],{"class":836},"syre3","out",[411,839,840],{"class":433}," chan",[411,842,599],{"class":464},[411,844,845],{"class":437}," int",[411,847,848],{"class":429},")",[411,850,517],{"class":429},[411,852,853,856,858],{"class":413,"line":483},[411,854,855],{"class":417},"    out",[411,857,465],{"class":464},[411,859,860],{"class":639}," 42\n",[411,862,863],{"class":413,"line":537},[411,864,609],{"class":429},[411,866,867],{"class":413,"line":544},[411,868,541],{"emptyLinePlaceholder":540},[411,870,871,873,876,878,881,883,885,887,889],{"class":413,"line":558},[411,872,508],{"class":433},[411,874,875],{"class":425}," consume",[411,877,430],{"class":429},[411,879,880],{"class":836},"in",[411,882,465],{"class":464},[411,884,434],{"class":433},[411,886,845],{"class":437},[411,888,848],{"class":429},[411,890,517],{"class":429},[411,892,893,895,897,899,901,903,905],{"class":413,"line":574},[411,894,588],{"class":417},[411,896,591],{"class":429},[411,898,594],{"class":425},[411,900,430],{"class":429},[411,902,599],{"class":464},[411,904,880],{"class":417},[411,906,441],{"class":429},[411,908,909],{"class":413,"line":580},[411,910,609],{"class":429},[394,912,913,916,917,920],{},[408,914,915],{},"chan\u003C- int"," is send-only. ",[408,918,919],{},"\u003C-chan int"," is receive-only.",[389,922,924],{"id":923},"channels-are-not-queues-by-default","Channels are not queues by default",[394,926,927],{},"An unbuffered channel has no storage. Even a buffered channel is usually best understood as a coordination mechanism with bounded storage, not a replacement for a durable job queue.",[389,929,931],{"id":930},"common-mistakes","Common mistakes",[394,933,934],{},"Sending and receiving on the same unbuffered channel in one goroutine deadlocks,\nbecause neither operation can meet its counterpart. Closing from a receiver can\nrace with active senders and make them panic. Adding a buffer may change the\ntiming of either failure, but it does not establish ownership or cancellation.\nFinally, a channel does not need to be closed merely because it exists; close\nit when receivers need an end-of-stream signal.",[389,936,938],{"id":937},"practice","Practice",[394,940,941],{},"Write a producer that sends the numbers 1 through 5 and then closes its channel. Write a consumer that ranges over the channel and prints the square of each number.",[943,944,945],"accordion",{},[946,947,950,951,954],"accordion-item",{"icon":948,"label":949},"i-lucide-lightbulb","Show solution","Create the channel, start producers, and have one owner close it only after every producer has stopped sending. Consumers can ",[408,952,953],{},"range"," until closure. Do not close a channel merely to signal that one receiver is finished.",[389,956,958],{"id":957},"summary","Summary",[394,960,961],{},"Channels combine communication with synchronization. Their capacity changes\nwhen a send blocks, so choose it to express a measured bound. The owner that\nknows all sends are finished closes the channel to announce end of stream.",[394,963,964,965,972,973,978,979,591],{},"Official references: ",[966,967,971],"a",{"href":968,"rel":969},"https:\u002F\u002Fgo.dev\u002Fref\u002Fspec#Channel_types",[970],"nofollow","channels in the language specification",", ",[966,974,977],{"href":975,"rel":976},"https:\u002F\u002Fgo.dev\u002Fref\u002Fspec#Send_statements",[970],"send statements",", and ",[966,980,983],{"href":981,"rel":982},"https:\u002F\u002Fgo.dev\u002Fref\u002Fmem",[970],"the Go memory model",[985,986],"learning-lesson-complete",{},[988,989],"learning-lesson-navigation",{"next":136,"nextTitle":990,"previous":126,"previousTitle":125},"Select and cancellation",[992,993,994],"style",{},"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 .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}html pre.shiki code .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}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 .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 .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}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 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}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 .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}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}",{"title":406,"searchDepth":483,"depth":483,"links":996},[997,998,999,1000,1001,1002,1003,1004,1005,1006],{"id":391,"depth":483,"text":392},{"id":451,"depth":483,"text":452},{"id":612,"depth":483,"text":613},{"id":677,"depth":483,"text":678},{"id":706,"depth":483,"text":707},{"id":815,"depth":483,"text":816},{"id":923,"depth":483,"text":924},{"id":930,"depth":483,"text":931},{"id":937,"depth":483,"text":938},{"id":957,"depth":483,"text":958},"Send values between goroutines and understand blocking, buffering, closing, and ownership.","md",null,{},{"icon":133},{"description":1013,"title":130},"Learn Go channels, blocking sends and receives, buffered channels, closing, range, and channel ownership.","ObXSUvXJeTbWaGFBncuUMVio9V0JQXtp9FAx6PZcSpw",[1016,1018],{"title":125,"path":126,"stem":127,"description":1017,"icon":128,"children":-1},"Start lightweight concurrent functions and manage their lifetime correctly.",{"title":135,"path":136,"stem":137,"description":1019,"icon":138,"children":-1},"Wait on multiple channel operations and stop concurrent work with context.",1794118099588]