[{"data":1,"prerenderedAt":1475},["ShallowReactive",2],{"navigation_docs":3,"-projects-file-backed-api-file-store-and-concurrency":377,"-projects-file-backed-api-file-store-and-concurrency-surround":1470},[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":340,"body":379,"description":1464,"extension":1465,"links":1466,"meta":1467,"navigation":632,"path":341,"seo":1468,"stem":342,"__hash__":1469},"docs\u002F7.projects\u002F3.file-backed-api\u002F1.file-store-and-concurrency.md",{"type":380,"value":381,"toc":1454},"minimark",[382,389,393,397,402,426,430,517,520,524,836,842,846,1367,1370,1374,1377,1398,1401,1405,1410,1414,1417,1426,1430,1444,1447,1450],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Build a file store whose in-process concurrency and crash limitations are explicit.","Intermediate","File-backed notes API design, JSON, files, and sync.RWMutex","60 minutes",[390,391,340],"h1",{"id":392},"file-store-and-concurrency",[394,395,396],"p",{},"The store keeps an in-memory map for fast access and persists the complete collection after each change. A mutex protects the map and prevents two writes from replacing the file at the same time.",[398,399,401],"h2",{"id":400},"what-you-will-learn","What you will learn",[403,404,405,409,417,420,423],"ul",{},[406,407,408],"li",{},"return copies instead of exposing mutable internal state;",[406,410,411,412,416],{},"use ",[413,414,415],"code",{},"sync.RWMutex"," around shared data;",[406,418,419],{},"write a temporary file and rename it into place;",[406,421,422],{},"preserve the last good snapshot when encoding fails;",[406,424,425],{},"describe the limits of a single-process file store.",[398,427,429],{"id":428},"store-shape","Store shape",[431,432,437],"pre",{"className":433,"code":434,"language":435,"meta":436,"style":436},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","type FileStore struct {\n    mu    sync.RWMutex\n    path  string\n    notes map[string]note.Note\n}\n","go","",[413,438,439,459,475,485,511],{"__ignoreMap":436},[440,441,444,448,452,455],"span",{"class":442,"line":443},"line",1,[440,445,447],{"class":446},"sEOdo","type",[440,449,451],{"class":450},"snwKR"," FileStore",[440,453,454],{"class":446}," struct",[440,456,458],{"class":457},"shFME"," {\n",[440,460,462,466,469,472],{"class":442,"line":461},2,[440,463,465],{"class":464},"s45-6","    mu",[440,467,468],{"class":450},"    sync",[440,470,471],{"class":457},".",[440,473,474],{"class":450},"RWMutex\n",[440,476,478,481],{"class":442,"line":477},3,[440,479,480],{"class":464},"    path",[440,482,484],{"class":483},"sF6sA","  string\n",[440,486,488,491,494,497,500,503,506,508],{"class":442,"line":487},4,[440,489,490],{"class":464},"    notes",[440,492,493],{"class":446}," map",[440,495,496],{"class":457},"[",[440,498,499],{"class":483},"string",[440,501,502],{"class":457},"]",[440,504,505],{"class":450},"note",[440,507,471],{"class":457},[440,509,510],{"class":450},"Note\n",[440,512,514],{"class":442,"line":513},5,[440,515,516],{"class":457},"}\n",[394,518,519],{},"Load and validate the file in a constructor. If the file does not exist, start with an empty map. Other errors—including invalid JSON—should be returned rather than silently discarding data.",[398,521,523],{"id":522},"return-a-snapshot","Return a snapshot",[431,525,527],{"className":433,"code":526,"language":435,"meta":436,"style":436},"func (s *FileStore) List(_ context.Context) ([]note.Note, error) {\n    s.mu.RLock()\n    defer s.mu.RUnlock()\n\n    result := make([]note.Note, 0, len(s.notes))\n    for _, n := range s.notes {\n        result = append(result, n)\n    }\n    slices.SortFunc(result, func(a, b note.Note) int {\n        return a.CreatedAt.Compare(b.CreatedAt)\n    })\n    return result, nil\n}\n",[413,528,529,590,608,628,634,679,706,730,736,780,810,816,831],{"__ignoreMap":436},[440,530,531,534,537,541,544,547,550,554,557,560,563,565,568,570,573,575,577,580,583,586,588],{"class":442,"line":443},[440,532,533],{"class":446},"func",[440,535,536],{"class":457}," (",[440,538,540],{"class":539},"syre3","s ",[440,542,543],{"class":446},"*",[440,545,546],{"class":450},"FileStore",[440,548,549],{"class":457},")",[440,551,553],{"class":552},"sisdC"," List",[440,555,556],{"class":457},"(",[440,558,559],{"class":539},"_",[440,561,562],{"class":450}," context",[440,564,471],{"class":457},[440,566,567],{"class":450},"Context",[440,569,549],{"class":457},[440,571,572],{"class":457}," ([]",[440,574,505],{"class":450},[440,576,471],{"class":457},[440,578,579],{"class":450},"Note",[440,581,582],{"class":457},",",[440,584,585],{"class":483}," error",[440,587,549],{"class":457},[440,589,458],{"class":457},[440,591,592,595,597,600,602,605],{"class":442,"line":461},[440,593,594],{"class":464},"    s",[440,596,471],{"class":457},[440,598,599],{"class":464},"mu",[440,601,471],{"class":457},[440,603,604],{"class":552},"RLock",[440,606,607],{"class":457},"()\n",[440,609,610,614,617,619,621,623,626],{"class":442,"line":477},[440,611,613],{"class":612},"s7Bcg","    defer",[440,615,616],{"class":464}," s",[440,618,471],{"class":457},[440,620,599],{"class":464},[440,622,471],{"class":457},[440,624,625],{"class":552},"RUnlock",[440,627,607],{"class":457},[440,629,630],{"class":442,"line":487},[440,631,633],{"emptyLinePlaceholder":632},true,"\n",[440,635,636,639,643,646,649,651,653,655,657,661,663,666,668,671,673,676],{"class":442,"line":513},[440,637,638],{"class":464},"    result",[440,640,642],{"class":641},"su2KV"," :=",[440,644,645],{"class":552}," make",[440,647,648],{"class":457},"([]",[440,650,505],{"class":450},[440,652,471],{"class":457},[440,654,579],{"class":450},[440,656,582],{"class":457},[440,658,660],{"class":659},"sAJzr"," 0",[440,662,582],{"class":457},[440,664,665],{"class":552}," len",[440,667,556],{"class":457},[440,669,670],{"class":464},"s",[440,672,471],{"class":457},[440,674,675],{"class":464},"notes",[440,677,678],{"class":457},"))\n",[440,680,682,685,688,690,693,695,698,700,702,704],{"class":442,"line":681},6,[440,683,684],{"class":612},"    for",[440,686,687],{"class":464}," _",[440,689,582],{"class":457},[440,691,692],{"class":464}," n",[440,694,642],{"class":641},[440,696,697],{"class":612}," range",[440,699,616],{"class":464},[440,701,471],{"class":457},[440,703,675],{"class":464},[440,705,458],{"class":457},[440,707,709,712,715,718,720,723,725,727],{"class":442,"line":708},7,[440,710,711],{"class":464},"        result",[440,713,714],{"class":641}," =",[440,716,717],{"class":552}," append",[440,719,556],{"class":457},[440,721,722],{"class":464},"result",[440,724,582],{"class":457},[440,726,692],{"class":464},[440,728,729],{"class":457},")\n",[440,731,733],{"class":442,"line":732},8,[440,734,735],{"class":457},"    }\n",[440,737,739,742,744,747,749,751,753,756,758,761,763,766,769,771,773,775,778],{"class":442,"line":738},9,[440,740,741],{"class":464},"    slices",[440,743,471],{"class":457},[440,745,746],{"class":552},"SortFunc",[440,748,556],{"class":457},[440,750,722],{"class":464},[440,752,582],{"class":457},[440,754,755],{"class":446}," func",[440,757,556],{"class":457},[440,759,760],{"class":539},"a",[440,762,582],{"class":457},[440,764,765],{"class":539}," b",[440,767,768],{"class":450}," note",[440,770,471],{"class":457},[440,772,579],{"class":450},[440,774,549],{"class":457},[440,776,777],{"class":483}," int",[440,779,458],{"class":457},[440,781,783,786,789,791,794,796,799,801,804,806,808],{"class":442,"line":782},10,[440,784,785],{"class":612},"        return",[440,787,788],{"class":464}," a",[440,790,471],{"class":457},[440,792,793],{"class":464},"CreatedAt",[440,795,471],{"class":457},[440,797,798],{"class":552},"Compare",[440,800,556],{"class":457},[440,802,803],{"class":464},"b",[440,805,471],{"class":457},[440,807,793],{"class":464},[440,809,729],{"class":457},[440,811,813],{"class":442,"line":812},11,[440,814,815],{"class":457},"    })\n",[440,817,819,822,825,827],{"class":442,"line":818},12,[440,820,821],{"class":612},"    return",[440,823,824],{"class":464}," result",[440,826,582],{"class":457},[440,828,830],{"class":829},"sp8Z5"," nil\n",[440,832,834],{"class":442,"line":833},13,[440,835,516],{"class":457},[394,837,838,839,841],{},"The returned slice is independent of the map. Because every ",[413,840,579],{}," field here is a value or immutable string, copying each struct is enough. A model containing maps, slices, or pointers would need deliberate deeper copying.",[398,843,845],{"id":844},"persist-without-truncating-the-good-file-first","Persist without truncating the good file first",[431,847,849],{"className":433,"code":848,"language":435,"meta":436,"style":436},"func writeSnapshot(path string, notes []note.Note) error {\n    directory := filepath.Dir(path)\n    temporary, err := os.CreateTemp(directory, \".notes-*.json\")\n    if err != nil {\n        return fmt.Errorf(\"create temporary snapshot: %w\", err)\n    }\n    temporaryName := temporary.Name()\n    defer os.Remove(temporaryName)\n\n    encoder := json.NewEncoder(temporary)\n    encoder.SetIndent(\"\", \"  \")\n    if err := encoder.Encode(notes); err != nil {\n        temporary.Close()\n        return fmt.Errorf(\"encode snapshot: %w\", err)\n    }\n    if err := temporary.Sync(); err != nil {\n        temporary.Close()\n        return fmt.Errorf(\"sync snapshot: %w\", err)\n    }\n    if err := temporary.Close(); err != nil {\n        return fmt.Errorf(\"close snapshot: %w\", err)\n    }\n    if err := os.Rename(temporaryName, path); err != nil {\n        return fmt.Errorf(\"replace snapshot: %w\", err)\n    }\n    return nil\n}\n",[413,850,851,886,907,947,963,994,998,1015,1033,1037,1059,1082,1113,1125,1153,1158,1185,1196,1224,1229,1254,1282,1287,1322,1350,1355,1362],{"__ignoreMap":436},[440,852,853,855,858,860,863,866,868,871,874,876,878,880,882,884],{"class":442,"line":443},[440,854,533],{"class":446},[440,856,857],{"class":552}," writeSnapshot",[440,859,556],{"class":457},[440,861,862],{"class":539},"path",[440,864,865],{"class":483}," string",[440,867,582],{"class":457},[440,869,870],{"class":539}," notes",[440,872,873],{"class":457}," []",[440,875,505],{"class":450},[440,877,471],{"class":457},[440,879,579],{"class":450},[440,881,549],{"class":457},[440,883,585],{"class":483},[440,885,458],{"class":457},[440,887,888,891,893,896,898,901,903,905],{"class":442,"line":461},[440,889,890],{"class":464},"    directory",[440,892,642],{"class":641},[440,894,895],{"class":464}," filepath",[440,897,471],{"class":457},[440,899,900],{"class":552},"Dir",[440,902,556],{"class":457},[440,904,862],{"class":464},[440,906,729],{"class":457},[440,908,909,912,914,917,919,922,924,927,929,932,934,938,942,945],{"class":442,"line":477},[440,910,911],{"class":464},"    temporary",[440,913,582],{"class":457},[440,915,916],{"class":464}," err",[440,918,642],{"class":641},[440,920,921],{"class":464}," os",[440,923,471],{"class":457},[440,925,926],{"class":552},"CreateTemp",[440,928,556],{"class":457},[440,930,931],{"class":464},"directory",[440,933,582],{"class":457},[440,935,937],{"class":936},"sED5a"," \"",[440,939,941],{"class":940},"sVazx",".notes-*.json",[440,943,944],{"class":936},"\"",[440,946,729],{"class":457},[440,948,949,952,954,958,961],{"class":442,"line":487},[440,950,951],{"class":612},"    if",[440,953,916],{"class":464},[440,955,957],{"class":956},"sRBoR"," !=",[440,959,960],{"class":829}," nil",[440,962,458],{"class":457},[440,964,965,967,970,972,975,977,979,982,986,988,990,992],{"class":442,"line":513},[440,966,785],{"class":612},[440,968,969],{"class":464}," fmt",[440,971,471],{"class":457},[440,973,974],{"class":552},"Errorf",[440,976,556],{"class":457},[440,978,944],{"class":936},[440,980,981],{"class":940},"create temporary snapshot: ",[440,983,985],{"class":984},"st3jj","%w",[440,987,944],{"class":936},[440,989,582],{"class":457},[440,991,916],{"class":464},[440,993,729],{"class":457},[440,995,996],{"class":442,"line":681},[440,997,735],{"class":457},[440,999,1000,1003,1005,1008,1010,1013],{"class":442,"line":708},[440,1001,1002],{"class":464},"    temporaryName",[440,1004,642],{"class":641},[440,1006,1007],{"class":464}," temporary",[440,1009,471],{"class":457},[440,1011,1012],{"class":552},"Name",[440,1014,607],{"class":457},[440,1016,1017,1019,1021,1023,1026,1028,1031],{"class":442,"line":732},[440,1018,613],{"class":612},[440,1020,921],{"class":464},[440,1022,471],{"class":457},[440,1024,1025],{"class":552},"Remove",[440,1027,556],{"class":457},[440,1029,1030],{"class":464},"temporaryName",[440,1032,729],{"class":457},[440,1034,1035],{"class":442,"line":738},[440,1036,633],{"emptyLinePlaceholder":632},[440,1038,1039,1042,1044,1047,1049,1052,1054,1057],{"class":442,"line":782},[440,1040,1041],{"class":464},"    encoder",[440,1043,642],{"class":641},[440,1045,1046],{"class":464}," json",[440,1048,471],{"class":457},[440,1050,1051],{"class":552},"NewEncoder",[440,1053,556],{"class":457},[440,1055,1056],{"class":464},"temporary",[440,1058,729],{"class":457},[440,1060,1061,1063,1065,1068,1070,1073,1075,1077,1080],{"class":442,"line":812},[440,1062,1041],{"class":464},[440,1064,471],{"class":457},[440,1066,1067],{"class":552},"SetIndent",[440,1069,556],{"class":457},[440,1071,1072],{"class":936},"\"\"",[440,1074,582],{"class":457},[440,1076,937],{"class":936},[440,1078,1079],{"class":936},"  \"",[440,1081,729],{"class":457},[440,1083,1084,1086,1088,1090,1093,1095,1098,1100,1102,1105,1107,1109,1111],{"class":442,"line":818},[440,1085,951],{"class":612},[440,1087,916],{"class":464},[440,1089,642],{"class":641},[440,1091,1092],{"class":464}," encoder",[440,1094,471],{"class":457},[440,1096,1097],{"class":552},"Encode",[440,1099,556],{"class":457},[440,1101,675],{"class":464},[440,1103,1104],{"class":457},");",[440,1106,916],{"class":464},[440,1108,957],{"class":956},[440,1110,960],{"class":829},[440,1112,458],{"class":457},[440,1114,1115,1118,1120,1123],{"class":442,"line":833},[440,1116,1117],{"class":464},"        temporary",[440,1119,471],{"class":457},[440,1121,1122],{"class":552},"Close",[440,1124,607],{"class":457},[440,1126,1128,1130,1132,1134,1136,1138,1140,1143,1145,1147,1149,1151],{"class":442,"line":1127},14,[440,1129,785],{"class":612},[440,1131,969],{"class":464},[440,1133,471],{"class":457},[440,1135,974],{"class":552},[440,1137,556],{"class":457},[440,1139,944],{"class":936},[440,1141,1142],{"class":940},"encode snapshot: ",[440,1144,985],{"class":984},[440,1146,944],{"class":936},[440,1148,582],{"class":457},[440,1150,916],{"class":464},[440,1152,729],{"class":457},[440,1154,1156],{"class":442,"line":1155},15,[440,1157,735],{"class":457},[440,1159,1161,1163,1165,1167,1169,1171,1174,1177,1179,1181,1183],{"class":442,"line":1160},16,[440,1162,951],{"class":612},[440,1164,916],{"class":464},[440,1166,642],{"class":641},[440,1168,1007],{"class":464},[440,1170,471],{"class":457},[440,1172,1173],{"class":552},"Sync",[440,1175,1176],{"class":457},"();",[440,1178,916],{"class":464},[440,1180,957],{"class":956},[440,1182,960],{"class":829},[440,1184,458],{"class":457},[440,1186,1188,1190,1192,1194],{"class":442,"line":1187},17,[440,1189,1117],{"class":464},[440,1191,471],{"class":457},[440,1193,1122],{"class":552},[440,1195,607],{"class":457},[440,1197,1199,1201,1203,1205,1207,1209,1211,1214,1216,1218,1220,1222],{"class":442,"line":1198},18,[440,1200,785],{"class":612},[440,1202,969],{"class":464},[440,1204,471],{"class":457},[440,1206,974],{"class":552},[440,1208,556],{"class":457},[440,1210,944],{"class":936},[440,1212,1213],{"class":940},"sync snapshot: ",[440,1215,985],{"class":984},[440,1217,944],{"class":936},[440,1219,582],{"class":457},[440,1221,916],{"class":464},[440,1223,729],{"class":457},[440,1225,1227],{"class":442,"line":1226},19,[440,1228,735],{"class":457},[440,1230,1232,1234,1236,1238,1240,1242,1244,1246,1248,1250,1252],{"class":442,"line":1231},20,[440,1233,951],{"class":612},[440,1235,916],{"class":464},[440,1237,642],{"class":641},[440,1239,1007],{"class":464},[440,1241,471],{"class":457},[440,1243,1122],{"class":552},[440,1245,1176],{"class":457},[440,1247,916],{"class":464},[440,1249,957],{"class":956},[440,1251,960],{"class":829},[440,1253,458],{"class":457},[440,1255,1257,1259,1261,1263,1265,1267,1269,1272,1274,1276,1278,1280],{"class":442,"line":1256},21,[440,1258,785],{"class":612},[440,1260,969],{"class":464},[440,1262,471],{"class":457},[440,1264,974],{"class":552},[440,1266,556],{"class":457},[440,1268,944],{"class":936},[440,1270,1271],{"class":940},"close snapshot: ",[440,1273,985],{"class":984},[440,1275,944],{"class":936},[440,1277,582],{"class":457},[440,1279,916],{"class":464},[440,1281,729],{"class":457},[440,1283,1285],{"class":442,"line":1284},22,[440,1286,735],{"class":457},[440,1288,1290,1292,1294,1296,1298,1300,1303,1305,1307,1309,1312,1314,1316,1318,1320],{"class":442,"line":1289},23,[440,1291,951],{"class":612},[440,1293,916],{"class":464},[440,1295,642],{"class":641},[440,1297,921],{"class":464},[440,1299,471],{"class":457},[440,1301,1302],{"class":552},"Rename",[440,1304,556],{"class":457},[440,1306,1030],{"class":464},[440,1308,582],{"class":457},[440,1310,1311],{"class":464}," path",[440,1313,1104],{"class":457},[440,1315,916],{"class":464},[440,1317,957],{"class":956},[440,1319,960],{"class":829},[440,1321,458],{"class":457},[440,1323,1325,1327,1329,1331,1333,1335,1337,1340,1342,1344,1346,1348],{"class":442,"line":1324},24,[440,1326,785],{"class":612},[440,1328,969],{"class":464},[440,1330,471],{"class":457},[440,1332,974],{"class":552},[440,1334,556],{"class":457},[440,1336,944],{"class":936},[440,1338,1339],{"class":940},"replace snapshot: ",[440,1341,985],{"class":984},[440,1343,944],{"class":936},[440,1345,582],{"class":457},[440,1347,916],{"class":464},[440,1349,729],{"class":457},[440,1351,1353],{"class":442,"line":1352},25,[440,1354,735],{"class":457},[440,1356,1358,1360],{"class":442,"line":1357},26,[440,1359,821],{"class":612},[440,1361,830],{"class":829},[440,1363,1365],{"class":442,"line":1364},27,[440,1366,516],{"class":457},[394,1368,1369],{},"Creating the temporary file in the destination directory makes a same-filesystem rename possible. Rename behaviour and durability details vary by operating system and filesystem. For stronger crash guarantees, directory syncing and platform-specific handling may be required; a database is usually the better next step.",[398,1371,1373],{"id":1372},"mutate-only-when-persistence-succeeds","Mutate only when persistence succeeds",[394,1375,1376],{},"A simple safe approach is:",[1378,1379,1380,1383,1386,1389,1392,1395],"ol",{},[406,1381,1382],{},"take the write lock;",[406,1384,1385],{},"copy the map;",[406,1387,1388],{},"apply the change to the copy;",[406,1390,1391],{},"persist the copy;",[406,1393,1394],{},"replace the in-memory map only after persistence succeeds;",[406,1396,1397],{},"release the lock.",[394,1399,1400],{},"Holding the lock across disk I\u002FO limits throughput, but makes the correctness rule easy to reason about. Optimise only after measurement and a design that preserves ordering.",[398,1402,1404],{"id":1403},"important-limitation","Important limitation",[394,1406,1407,1409],{},[413,1408,415],{}," coordinates goroutines in one process only. Starting two copies of this API against the same file can lose updates. This project must be documented as single-process. Multi-process writers need file locking with platform caveats or, preferably, a transactional database.",[398,1411,1413],{"id":1412},"practice","Practice",[394,1415,1416],{},"Why is “update the live map, unlock, then write the file” unsafe even if every individual map access uses a mutex?",[1418,1419,1420],"accordion",{},[1421,1422,1425],"accordion-item",{"icon":1423,"label":1424},"i-lucide-lightbulb","Show solution","Another request can observe or build on the new in-memory state before persistence succeeds. Two writers can also complete file writes in a different order from their map updates. If a write fails, memory and disk disagree. Serialising the entire copy–persist–publish operation gives the store one clear commit point.",[398,1427,1429],{"id":1428},"summary","Summary",[403,1431,1432,1435,1438,1441],{},[406,1433,1434],{},"Protect shared maps and return independent snapshots.",[406,1436,1437],{},"Preserve the old file by writing a temporary snapshot first.",[406,1439,1440],{},"Publish the in-memory change only after persistence succeeds.",[406,1442,1443],{},"State clearly that an in-process mutex does not coordinate multiple processes.",[1445,1446],"learning-lesson-complete",{},[1448,1449],"learning-lesson-navigation",{"next":345,"nextTitle":344,"previous":334,"previousTitle":338},[1451,1452,1453],"style",{},"html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}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 .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}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 .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 .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}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 .sp8Z5, html code.shiki .sp8Z5{--shiki-light:#B58900;--shiki-default:#89DDFF;--shiki-dark:#D19A66}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 .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .st3jj, html code.shiki .st3jj{--shiki-light:#CB4B16;--shiki-default:#F07178;--shiki-dark:#D19A66}",{"title":436,"searchDepth":461,"depth":461,"links":1455},[1456,1457,1458,1459,1460,1461,1462,1463],{"id":400,"depth":461,"text":401},{"id":428,"depth":461,"text":429},{"id":522,"depth":461,"text":523},{"id":844,"depth":461,"text":845},{"id":1372,"depth":461,"text":1373},{"id":1403,"depth":461,"text":1404},{"id":1412,"depth":461,"text":1413},{"id":1428,"depth":461,"text":1429},"Implement snapshot reads, serialized writes, and recoverable file replacement for the notes API.","md",null,{},{"title":340,"description":1464},"QfOxuG5MhYwHYK8bYrE5t2jRV8vLcKO4jX_LGKH7vr4",[1471,1473],{"title":338,"path":334,"stem":335,"description":1472,"children":-1},"Design an intermediate HTTP API with clear layers, durable JSON storage, and predictable errors.",{"title":344,"path":345,"stem":346,"description":1474,"children":-1},"Finish the API with bounded decoding, stable errors, and tests at each boundary.",1794118105931]