[{"data":1,"prerenderedAt":2738},["ShallowReactive",2],{"navigation_docs":3,"-fundamentals-errors":377,"-fundamentals-errors-surround":2733},[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":60,"body":379,"description":2725,"extension":2726,"links":2727,"meta":2728,"navigation":2729,"path":61,"seo":2730,"stem":62,"__hash__":2732},"docs\u002F2.fundamentals\u002F12.errors.md",{"type":380,"value":381,"toc":2706},"minimark",[382,389,394,411,415,426,571,589,596,601,635,642,646,653,768,774,815,821,825,828,914,925,929,940,1062,1081,1084,1088,1091,1098,1183,1193,1200,1312,1322,1326,1332,1449,1456,1460,1463,1655,1658,1754,1758,1761,1777,1780,1784,1791,1871,1878,1884,1893,1963,1970,1974,2030,2034,2048,2652,2656,2682,2696,2699,2702],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Handle failure the Go way by returning, wrapping, and inspecting errors.","Intermediate","Interfaces, Functions","45 minutes",[390,391,393],"h2",{"id":392},"what-you-will-learn","What you will learn",[395,396,397,398,402,403,406,407,410],"p",{},"Programs fail: files go missing, networks drop, users type nonsense. Go handles failure with ordinary values rather than exceptions. This lesson covers how to return and check errors, how to add context by wrapping, how to test for specific errors with ",[399,400,401],"code",{},"errors.Is"," and ",[399,404,405],{},"errors.As",", how to define your own errors, and where ",[399,408,409],{},"panic"," fits in.",[390,412,414],{"id":413},"errors-are-values","Errors are values",[395,416,417,418,421,422,425],{},"Many languages use ",[399,419,420],{},"try\u002Fcatch",", which jumps out of the current code when something fails. Go has no exceptions. A function that can fail returns an ",[399,423,424],{},"error"," as its last result, and the caller checks it:",[427,428,433],"pre",{"className":429,"code":430,"language":431,"meta":432,"style":432},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","data, err := os.ReadFile(\"config.json\")\nif err != nil {\n    fmt.Println(\"could not read config:\", err)\n    return\n}\nfmt.Println(len(data), \"bytes read\")\n","go","",[399,434,435,481,501,527,533,539],{"__ignoreMap":432},[436,437,440,444,448,451,455,458,461,465,468,472,476,478],"span",{"class":438,"line":439},"line",1,[436,441,443],{"class":442},"s45-6","data",[436,445,447],{"class":446},"shFME",",",[436,449,450],{"class":442}," err",[436,452,454],{"class":453},"su2KV"," :=",[436,456,457],{"class":442}," os",[436,459,460],{"class":446},".",[436,462,464],{"class":463},"sisdC","ReadFile",[436,466,467],{"class":446},"(",[436,469,471],{"class":470},"sED5a","\"",[436,473,475],{"class":474},"sVazx","config.json",[436,477,471],{"class":470},[436,479,480],{"class":446},")\n",[436,482,484,488,490,494,498],{"class":438,"line":483},2,[436,485,487],{"class":486},"s7Bcg","if",[436,489,450],{"class":442},[436,491,493],{"class":492},"sRBoR"," !=",[436,495,497],{"class":496},"sp8Z5"," nil",[436,499,500],{"class":446}," {\n",[436,502,504,507,509,512,514,516,519,521,523,525],{"class":438,"line":503},3,[436,505,506],{"class":442},"    fmt",[436,508,460],{"class":446},[436,510,511],{"class":463},"Println",[436,513,467],{"class":446},[436,515,471],{"class":470},[436,517,518],{"class":474},"could not read config:",[436,520,471],{"class":470},[436,522,447],{"class":446},[436,524,450],{"class":442},[436,526,480],{"class":446},[436,528,530],{"class":438,"line":529},4,[436,531,532],{"class":486},"    return\n",[436,534,536],{"class":438,"line":535},5,[436,537,538],{"class":446},"}\n",[436,540,542,545,547,549,551,554,556,558,561,564,567,569],{"class":438,"line":541},6,[436,543,544],{"class":442},"fmt",[436,546,460],{"class":446},[436,548,511],{"class":463},[436,550,467],{"class":446},[436,552,553],{"class":463},"len",[436,555,467],{"class":446},[436,557,443],{"class":442},[436,559,560],{"class":446},"),",[436,562,563],{"class":470}," \"",[436,565,566],{"class":474},"bytes read",[436,568,471],{"class":470},[436,570,480],{"class":446},[395,572,573,574,577,578,581,582,584,585,588],{},"If ",[399,575,576],{},"err"," is ",[399,579,580],{},"nil",", the call worked. If it is not ",[399,583,580],{},", something went wrong and the other results should not be trusted. You will write ",[399,586,587],{},"if err != nil"," many times, and that is by design: every place that can fail is visible in the code, right next to the success path.",[390,590,592,593,595],{"id":591},"the-error-interface","The ",[399,594,424],{}," interface",[395,597,598,600],{},[399,599,424],{}," is a tiny built-in interface:",[427,602,604],{"className":429,"code":603,"language":431,"meta":432,"style":432},"type error interface {\n    Error() string\n}\n",[399,605,606,620,631],{"__ignoreMap":432},[436,607,608,612,616,618],{"class":438,"line":439},[436,609,611],{"class":610},"sEOdo","type",[436,613,615],{"class":614},"sF6sA"," error",[436,617,595],{"class":610},[436,619,500],{"class":446},[436,621,622,625,628],{"class":438,"line":483},[436,623,624],{"class":463},"    Error",[436,626,627],{"class":446},"()",[436,629,630],{"class":614}," string\n",[436,632,633],{"class":438,"line":503},[436,634,538],{"class":446},[395,636,637,638,641],{},"Any type with an ",[399,639,640],{},"Error() string"," method is an error. That is the entire mechanism, and it uses what you learned in the interfaces lesson.",[390,643,645],{"id":644},"creating-errors","Creating errors",[395,647,648,649,652],{},"The simplest way is ",[399,650,651],{},"errors.New",":",[427,654,656],{"className":429,"code":655,"language":431,"meta":432,"style":432},"func divide(a, b float64) (float64, error) {\n    if b == 0 {\n        return 0, errors.New(\"divisor must not be zero\")\n    }\n    return a \u002F b, nil\n}\n",[399,657,658,697,713,741,746,764],{"__ignoreMap":432},[436,659,660,663,666,668,672,674,677,680,683,686,689,691,693,695],{"class":438,"line":439},[436,661,662],{"class":610},"func",[436,664,665],{"class":463}," divide",[436,667,467],{"class":446},[436,669,671],{"class":670},"syre3","a",[436,673,447],{"class":446},[436,675,676],{"class":670}," b",[436,678,679],{"class":614}," float64",[436,681,682],{"class":446},")",[436,684,685],{"class":446}," (",[436,687,688],{"class":614},"float64",[436,690,447],{"class":446},[436,692,615],{"class":614},[436,694,682],{"class":446},[436,696,500],{"class":446},[436,698,699,702,704,707,711],{"class":438,"line":483},[436,700,701],{"class":486},"    if",[436,703,676],{"class":442},[436,705,706],{"class":492}," ==",[436,708,710],{"class":709},"sAJzr"," 0",[436,712,500],{"class":446},[436,714,715,718,720,722,725,727,730,732,734,737,739],{"class":438,"line":503},[436,716,717],{"class":486},"        return",[436,719,710],{"class":709},[436,721,447],{"class":446},[436,723,724],{"class":442}," errors",[436,726,460],{"class":446},[436,728,729],{"class":463},"New",[436,731,467],{"class":446},[436,733,471],{"class":470},[436,735,736],{"class":474},"divisor must not be zero",[436,738,471],{"class":470},[436,740,480],{"class":446},[436,742,743],{"class":438,"line":529},[436,744,745],{"class":446},"    }\n",[436,747,748,751,754,757,759,761],{"class":438,"line":535},[436,749,750],{"class":486},"    return",[436,752,753],{"class":442}," a",[436,755,756],{"class":610}," \u002F",[436,758,676],{"class":442},[436,760,447],{"class":446},[436,762,763],{"class":496}," nil\n",[436,765,766],{"class":438,"line":541},[436,767,538],{"class":446},[395,769,770,771,652],{},"For a message with values in it, use ",[399,772,773],{},"fmt.Errorf",[427,775,777],{"className":429,"code":776,"language":431,"meta":432,"style":432},"return fmt.Errorf(\"user %d not found\", id)\n",[399,778,779],{"__ignoreMap":432},[436,780,781,784,787,789,792,794,796,799,803,806,808,810,813],{"class":438,"line":439},[436,782,783],{"class":486},"return",[436,785,786],{"class":442}," fmt",[436,788,460],{"class":446},[436,790,791],{"class":463},"Errorf",[436,793,467],{"class":446},[436,795,471],{"class":470},[436,797,798],{"class":474},"user ",[436,800,802],{"class":801},"st3jj","%d",[436,804,805],{"class":474}," not found",[436,807,471],{"class":470},[436,809,447],{"class":446},[436,811,812],{"class":442}," id",[436,814,480],{"class":446},[395,816,817,818,460],{},"By convention, error messages start with a lowercase letter and do not end with punctuation. This is because they often get joined into longer messages such as ",[399,819,820],{},"load profile: user 42 not found",[390,822,824],{"id":823},"checking-errors","Checking errors",[395,826,827],{},"Handle the error immediately, before using any other result:",[427,829,831],{"className":429,"code":830,"language":431,"meta":432,"style":432},"result, err := divide(10, 0)\nif err != nil {\n    fmt.Println(\"error:\", err)\n    return\n}\nfmt.Println(result)\n",[399,832,833,857,869,892,896,900],{"__ignoreMap":432},[436,834,835,838,840,842,844,846,848,851,853,855],{"class":438,"line":439},[436,836,837],{"class":442},"result",[436,839,447],{"class":446},[436,841,450],{"class":442},[436,843,454],{"class":453},[436,845,665],{"class":463},[436,847,467],{"class":446},[436,849,850],{"class":709},"10",[436,852,447],{"class":446},[436,854,710],{"class":709},[436,856,480],{"class":446},[436,858,859,861,863,865,867],{"class":438,"line":483},[436,860,487],{"class":486},[436,862,450],{"class":442},[436,864,493],{"class":492},[436,866,497],{"class":496},[436,868,500],{"class":446},[436,870,871,873,875,877,879,881,884,886,888,890],{"class":438,"line":503},[436,872,506],{"class":442},[436,874,460],{"class":446},[436,876,511],{"class":463},[436,878,467],{"class":446},[436,880,471],{"class":470},[436,882,883],{"class":474},"error:",[436,885,471],{"class":470},[436,887,447],{"class":446},[436,889,450],{"class":442},[436,891,480],{"class":446},[436,893,894],{"class":438,"line":529},[436,895,532],{"class":486},[436,897,898],{"class":438,"line":535},[436,899,538],{"class":446},[436,901,902,904,906,908,910,912],{"class":438,"line":541},[436,903,544],{"class":442},[436,905,460],{"class":446},[436,907,511],{"class":463},[436,909,467],{"class":446},[436,911,837],{"class":442},[436,913,480],{"class":446},[395,915,916,917,920,921,924],{},"It is tempting to ignore an error with ",[399,918,919],{},"_",", as in ",[399,922,923],{},"result, _ := divide(10, 0)",". Do that only when you are sure failure is impossible or harmless, because silent failures are very hard to debug.",[390,926,928],{"id":927},"adding-context-by-wrapping","Adding context by wrapping",[395,930,931,932,935,936,939],{},"A bare error such as ",[399,933,934],{},"open config.json: no such file or directory"," tells you what failed, but not what your program was trying to do. As an error travels up through functions, each layer can add context with the ",[399,937,938],{},"%w"," verb:",[427,941,943],{"className":429,"code":942,"language":431,"meta":432,"style":432},"func loadConfig(path string) ([]byte, error) {\n    data, err := os.ReadFile(path)\n    if err != nil {\n        return nil, fmt.Errorf(\"load config: %w\", err)\n    }\n    return data, nil\n}\n",[399,944,945,976,999,1011,1042,1046,1057],{"__ignoreMap":432},[436,946,947,949,952,954,957,960,962,965,968,970,972,974],{"class":438,"line":439},[436,948,662],{"class":610},[436,950,951],{"class":463}," loadConfig",[436,953,467],{"class":446},[436,955,956],{"class":670},"path",[436,958,959],{"class":614}," string",[436,961,682],{"class":446},[436,963,964],{"class":446}," ([]",[436,966,967],{"class":614},"byte",[436,969,447],{"class":446},[436,971,615],{"class":614},[436,973,682],{"class":446},[436,975,500],{"class":446},[436,977,978,981,983,985,987,989,991,993,995,997],{"class":438,"line":483},[436,979,980],{"class":442},"    data",[436,982,447],{"class":446},[436,984,450],{"class":442},[436,986,454],{"class":453},[436,988,457],{"class":442},[436,990,460],{"class":446},[436,992,464],{"class":463},[436,994,467],{"class":446},[436,996,956],{"class":442},[436,998,480],{"class":446},[436,1000,1001,1003,1005,1007,1009],{"class":438,"line":503},[436,1002,701],{"class":486},[436,1004,450],{"class":442},[436,1006,493],{"class":492},[436,1008,497],{"class":496},[436,1010,500],{"class":446},[436,1012,1013,1015,1017,1019,1021,1023,1025,1027,1029,1032,1034,1036,1038,1040],{"class":438,"line":529},[436,1014,717],{"class":486},[436,1016,497],{"class":496},[436,1018,447],{"class":446},[436,1020,786],{"class":442},[436,1022,460],{"class":446},[436,1024,791],{"class":463},[436,1026,467],{"class":446},[436,1028,471],{"class":470},[436,1030,1031],{"class":474},"load config: ",[436,1033,938],{"class":801},[436,1035,471],{"class":470},[436,1037,447],{"class":446},[436,1039,450],{"class":442},[436,1041,480],{"class":446},[436,1043,1044],{"class":438,"line":535},[436,1045,745],{"class":446},[436,1047,1048,1050,1053,1055],{"class":438,"line":541},[436,1049,750],{"class":486},[436,1051,1052],{"class":442}," data",[436,1054,447],{"class":446},[436,1056,763],{"class":496},[436,1058,1060],{"class":438,"line":1059},7,[436,1061,538],{"class":446},[395,1063,1064,1065,1068,1069,1071,1072,1076,1077,1080],{},"The resulting message reads like a trail: ",[399,1066,1067],{},"load config: open settings.json: no such file or directory",". Crucially, ",[399,1070,938],{}," ",[1073,1074,1075],"strong",{},"wraps"," the original error rather than just copying its text, so the original is still available for inspection. If you use ",[399,1078,1079],{},"%v"," instead, the message looks the same but the connection is lost.",[395,1082,1083],{},"A good context message says what operation failed and, where useful, which input was involved.",[390,1085,1087],{"id":1086},"inspecting-errors","Inspecting errors",[395,1089,1090],{},"Never compare errors by their message text. Messages are for humans and may change. Use these two functions instead.",[395,1092,1093,1097],{},[1073,1094,1095],{},[399,1096,401],{}," asks \"is this error, or anything it wraps, this particular error?\":",[427,1099,1101],{"className":429,"code":1100,"language":431,"meta":432,"style":432},"_, err := loadConfig(\"missing.json\")\n\nif errors.Is(err, os.ErrNotExist) {\n    fmt.Println(\"config file does not exist, using defaults\")\n}\n",[399,1102,1103,1126,1132,1160,1179],{"__ignoreMap":432},[436,1104,1105,1107,1109,1111,1113,1115,1117,1119,1122,1124],{"class":438,"line":439},[436,1106,919],{"class":442},[436,1108,447],{"class":446},[436,1110,450],{"class":442},[436,1112,454],{"class":453},[436,1114,951],{"class":463},[436,1116,467],{"class":446},[436,1118,471],{"class":470},[436,1120,1121],{"class":474},"missing.json",[436,1123,471],{"class":470},[436,1125,480],{"class":446},[436,1127,1128],{"class":438,"line":483},[436,1129,1131],{"emptyLinePlaceholder":1130},true,"\n",[436,1133,1134,1136,1138,1140,1143,1145,1147,1149,1151,1153,1156,1158],{"class":438,"line":503},[436,1135,487],{"class":486},[436,1137,724],{"class":442},[436,1139,460],{"class":446},[436,1141,1142],{"class":463},"Is",[436,1144,467],{"class":446},[436,1146,576],{"class":442},[436,1148,447],{"class":446},[436,1150,457],{"class":442},[436,1152,460],{"class":446},[436,1154,1155],{"class":442},"ErrNotExist",[436,1157,682],{"class":446},[436,1159,500],{"class":446},[436,1161,1162,1164,1166,1168,1170,1172,1175,1177],{"class":438,"line":529},[436,1163,506],{"class":442},[436,1165,460],{"class":446},[436,1167,511],{"class":463},[436,1169,467],{"class":446},[436,1171,471],{"class":470},[436,1173,1174],{"class":474},"config file does not exist, using defaults",[436,1176,471],{"class":470},[436,1178,480],{"class":446},[436,1180,1181],{"class":438,"line":535},[436,1182,538],{"class":446},[395,1184,1185,1186,1189,1190,1192],{},"It works even though ",[399,1187,1188],{},"loadConfig"," wrapped the original error, because ",[399,1191,401],{}," unwraps layer by layer.",[395,1194,1195,1199],{},[1073,1196,1197],{},[399,1198,405],{}," asks \"is there an error of this type in the chain?\", and gives you access to it:",[427,1201,1203],{"className":429,"code":1202,"language":431,"meta":432,"style":432},"var pathErr *fs.PathError\nif errors.As(err, &pathErr) {\n    fmt.Println(\"operation:\", pathErr.Op)\n    fmt.Println(\"path:\", pathErr.Path)\n}\n",[399,1204,1205,1225,1252,1280,1308],{"__ignoreMap":432},[436,1206,1207,1210,1213,1216,1220,1222],{"class":438,"line":439},[436,1208,1209],{"class":610},"var",[436,1211,1212],{"class":442}," pathErr",[436,1214,1215],{"class":610}," *",[436,1217,1219],{"class":1218},"snwKR","fs",[436,1221,460],{"class":446},[436,1223,1224],{"class":1218},"PathError\n",[436,1226,1227,1229,1231,1233,1236,1238,1240,1242,1245,1248,1250],{"class":438,"line":483},[436,1228,487],{"class":486},[436,1230,724],{"class":442},[436,1232,460],{"class":446},[436,1234,1235],{"class":463},"As",[436,1237,467],{"class":446},[436,1239,576],{"class":442},[436,1241,447],{"class":446},[436,1243,1244],{"class":610}," &",[436,1246,1247],{"class":442},"pathErr",[436,1249,682],{"class":446},[436,1251,500],{"class":446},[436,1253,1254,1256,1258,1260,1262,1264,1267,1269,1271,1273,1275,1278],{"class":438,"line":503},[436,1255,506],{"class":442},[436,1257,460],{"class":446},[436,1259,511],{"class":463},[436,1261,467],{"class":446},[436,1263,471],{"class":470},[436,1265,1266],{"class":474},"operation:",[436,1268,471],{"class":470},[436,1270,447],{"class":446},[436,1272,1212],{"class":442},[436,1274,460],{"class":446},[436,1276,1277],{"class":442},"Op",[436,1279,480],{"class":446},[436,1281,1282,1284,1286,1288,1290,1292,1295,1297,1299,1301,1303,1306],{"class":438,"line":529},[436,1283,506],{"class":442},[436,1285,460],{"class":446},[436,1287,511],{"class":463},[436,1289,467],{"class":446},[436,1291,471],{"class":470},[436,1293,1294],{"class":474},"path:",[436,1296,471],{"class":470},[436,1298,447],{"class":446},[436,1300,1212],{"class":442},[436,1302,460],{"class":446},[436,1304,1305],{"class":442},"Path",[436,1307,480],{"class":446},[436,1309,1310],{"class":438,"line":535},[436,1311,538],{"class":446},[395,1313,1314,1315,1318,1319,1321],{},"Note the ",[399,1316,1317],{},"&pathErr",": ",[399,1320,405],{}," needs a pointer so it can fill the variable in.",[390,1323,1325],{"id":1324},"sentinel-errors","Sentinel errors",[395,1327,1328,1329,652],{},"A package can export a fixed error value for callers to check, called a ",[1073,1330,1331],{},"sentinel error",[427,1333,1335],{"className":429,"code":1334,"language":431,"meta":432,"style":432},"var ErrNotFound = errors.New(\"not found\")\n\nfunc findUser(id int) (User, error) {\n    \u002F\u002F ...\n    return User{}, fmt.Errorf(\"find user %d: %w\", id, ErrNotFound)\n}\n",[399,1336,1337,1364,1368,1398,1404,1445],{"__ignoreMap":432},[436,1338,1339,1341,1344,1347,1349,1351,1353,1355,1357,1360,1362],{"class":438,"line":439},[436,1340,1209],{"class":610},[436,1342,1343],{"class":442}," ErrNotFound",[436,1345,1346],{"class":453}," =",[436,1348,724],{"class":442},[436,1350,460],{"class":446},[436,1352,729],{"class":463},[436,1354,467],{"class":446},[436,1356,471],{"class":470},[436,1358,1359],{"class":474},"not found",[436,1361,471],{"class":470},[436,1363,480],{"class":446},[436,1365,1366],{"class":438,"line":483},[436,1367,1131],{"emptyLinePlaceholder":1130},[436,1369,1370,1372,1375,1377,1380,1383,1385,1387,1390,1392,1394,1396],{"class":438,"line":503},[436,1371,662],{"class":610},[436,1373,1374],{"class":463}," findUser",[436,1376,467],{"class":446},[436,1378,1379],{"class":670},"id",[436,1381,1382],{"class":614}," int",[436,1384,682],{"class":446},[436,1386,685],{"class":446},[436,1388,1389],{"class":1218},"User",[436,1391,447],{"class":446},[436,1393,615],{"class":614},[436,1395,682],{"class":446},[436,1397,500],{"class":446},[436,1399,1400],{"class":438,"line":529},[436,1401,1403],{"class":1402},"sJsHy","    \u002F\u002F ...\n",[436,1405,1406,1408,1411,1414,1416,1418,1420,1422,1424,1427,1429,1431,1433,1435,1437,1439,1441,1443],{"class":438,"line":535},[436,1407,750],{"class":486},[436,1409,1410],{"class":1218}," User",[436,1412,1413],{"class":446},"{},",[436,1415,786],{"class":442},[436,1417,460],{"class":446},[436,1419,791],{"class":463},[436,1421,467],{"class":446},[436,1423,471],{"class":470},[436,1425,1426],{"class":474},"find user ",[436,1428,802],{"class":801},[436,1430,1318],{"class":474},[436,1432,938],{"class":801},[436,1434,471],{"class":470},[436,1436,447],{"class":446},[436,1438,812],{"class":442},[436,1440,447],{"class":446},[436,1442,1343],{"class":442},[436,1444,480],{"class":446},[436,1446,1447],{"class":438,"line":541},[436,1448,538],{"class":446},[395,1450,1451,1452,1455],{},"Callers test it with ",[399,1453,1454],{},"errors.Is(err, ErrNotFound)",". Sentinel errors are part of your package's public API, so create them only for conditions callers truly need to tell apart.",[390,1457,1459],{"id":1458},"custom-error-types","Custom error types",[395,1461,1462],{},"When callers need structured details, define a type:",[427,1464,1466],{"className":429,"code":1465,"language":431,"meta":432,"style":432},"type ValidationError struct {\n    Field   string\n    Message string\n}\n\nfunc (e ValidationError) Error() string {\n    return e.Field + \": \" + e.Message\n}\n\nfunc validateAge(age int) error {\n    if age \u003C 0 {\n        return ValidationError{Field: \"age\", Message: \"must not be negative\"}\n    }\n    return nil\n}\n",[399,1467,1468,1480,1488,1495,1499,1503,1526,1556,1561,1566,1587,1602,1638,1643,1650],{"__ignoreMap":432},[436,1469,1470,1472,1475,1478],{"class":438,"line":439},[436,1471,611],{"class":610},[436,1473,1474],{"class":1218}," ValidationError",[436,1476,1477],{"class":610}," struct",[436,1479,500],{"class":446},[436,1481,1482,1485],{"class":438,"line":483},[436,1483,1484],{"class":442},"    Field",[436,1486,1487],{"class":614},"   string\n",[436,1489,1490,1493],{"class":438,"line":503},[436,1491,1492],{"class":442},"    Message",[436,1494,630],{"class":614},[436,1496,1497],{"class":438,"line":529},[436,1498,538],{"class":446},[436,1500,1501],{"class":438,"line":535},[436,1502,1131],{"emptyLinePlaceholder":1130},[436,1504,1505,1507,1509,1512,1515,1517,1520,1522,1524],{"class":438,"line":541},[436,1506,662],{"class":610},[436,1508,685],{"class":446},[436,1510,1511],{"class":670},"e ",[436,1513,1514],{"class":1218},"ValidationError",[436,1516,682],{"class":446},[436,1518,1519],{"class":463}," Error",[436,1521,627],{"class":446},[436,1523,959],{"class":614},[436,1525,500],{"class":446},[436,1527,1528,1530,1533,1535,1538,1541,1543,1545,1547,1549,1551,1553],{"class":438,"line":1059},[436,1529,750],{"class":486},[436,1531,1532],{"class":442}," e",[436,1534,460],{"class":446},[436,1536,1537],{"class":442},"Field",[436,1539,1540],{"class":610}," +",[436,1542,563],{"class":470},[436,1544,1318],{"class":474},[436,1546,471],{"class":470},[436,1548,1540],{"class":610},[436,1550,1532],{"class":442},[436,1552,460],{"class":446},[436,1554,1555],{"class":442},"Message\n",[436,1557,1559],{"class":438,"line":1558},8,[436,1560,538],{"class":446},[436,1562,1564],{"class":438,"line":1563},9,[436,1565,1131],{"emptyLinePlaceholder":1130},[436,1567,1569,1571,1574,1576,1579,1581,1583,1585],{"class":438,"line":1568},10,[436,1570,662],{"class":610},[436,1572,1573],{"class":463}," validateAge",[436,1575,467],{"class":446},[436,1577,1578],{"class":670},"age",[436,1580,1382],{"class":614},[436,1582,682],{"class":446},[436,1584,615],{"class":614},[436,1586,500],{"class":446},[436,1588,1590,1592,1595,1598,1600],{"class":438,"line":1589},11,[436,1591,701],{"class":486},[436,1593,1594],{"class":442}," age",[436,1596,1597],{"class":492}," \u003C",[436,1599,710],{"class":709},[436,1601,500],{"class":446},[436,1603,1605,1607,1609,1612,1614,1616,1618,1620,1622,1624,1627,1629,1631,1634,1636],{"class":438,"line":1604},12,[436,1606,717],{"class":486},[436,1608,1474],{"class":1218},[436,1610,1611],{"class":446},"{",[436,1613,1537],{"class":442},[436,1615,652],{"class":446},[436,1617,563],{"class":470},[436,1619,1578],{"class":474},[436,1621,471],{"class":470},[436,1623,447],{"class":446},[436,1625,1626],{"class":442}," Message",[436,1628,652],{"class":446},[436,1630,563],{"class":470},[436,1632,1633],{"class":474},"must not be negative",[436,1635,471],{"class":470},[436,1637,538],{"class":446},[436,1639,1641],{"class":438,"line":1640},13,[436,1642,745],{"class":446},[436,1644,1646,1648],{"class":438,"line":1645},14,[436,1647,750],{"class":486},[436,1649,763],{"class":496},[436,1651,1653],{"class":438,"line":1652},15,[436,1654,538],{"class":446},[395,1656,1657],{},"A caller can then pull out the details:",[427,1659,1661],{"className":429,"code":1660,"language":431,"meta":432,"style":432},"err := validateAge(-5)\n\nvar ve ValidationError\nif errors.As(err, &ve) {\n    fmt.Println(\"bad field:\", ve.Field) \u002F\u002F bad field: age\n}\n",[399,1662,1663,1681,1685,1695,1720,1750],{"__ignoreMap":432},[436,1664,1665,1667,1669,1671,1673,1676,1679],{"class":438,"line":439},[436,1666,576],{"class":442},[436,1668,454],{"class":453},[436,1670,1573],{"class":463},[436,1672,467],{"class":446},[436,1674,1675],{"class":610},"-",[436,1677,1678],{"class":709},"5",[436,1680,480],{"class":446},[436,1682,1683],{"class":438,"line":483},[436,1684,1131],{"emptyLinePlaceholder":1130},[436,1686,1687,1689,1692],{"class":438,"line":503},[436,1688,1209],{"class":610},[436,1690,1691],{"class":442}," ve",[436,1693,1694],{"class":1218}," ValidationError\n",[436,1696,1697,1699,1701,1703,1705,1707,1709,1711,1713,1716,1718],{"class":438,"line":529},[436,1698,487],{"class":486},[436,1700,724],{"class":442},[436,1702,460],{"class":446},[436,1704,1235],{"class":463},[436,1706,467],{"class":446},[436,1708,576],{"class":442},[436,1710,447],{"class":446},[436,1712,1244],{"class":610},[436,1714,1715],{"class":442},"ve",[436,1717,682],{"class":446},[436,1719,500],{"class":446},[436,1721,1722,1724,1726,1728,1730,1732,1735,1737,1739,1741,1743,1745,1747],{"class":438,"line":535},[436,1723,506],{"class":442},[436,1725,460],{"class":446},[436,1727,511],{"class":463},[436,1729,467],{"class":446},[436,1731,471],{"class":470},[436,1733,1734],{"class":474},"bad field:",[436,1736,471],{"class":470},[436,1738,447],{"class":446},[436,1740,1691],{"class":442},[436,1742,460],{"class":446},[436,1744,1537],{"class":442},[436,1746,682],{"class":446},[436,1748,1749],{"class":1402}," \u002F\u002F bad field: age\n",[436,1751,1752],{"class":438,"line":541},[436,1753,538],{"class":446},[390,1755,1757],{"id":1756},"handle-it-or-return-it-not-both","Handle it or return it, not both",[395,1759,1760],{},"A common mistake is to log an error and also return it, so every layer logs the same failure again. Pick one job per layer:",[1762,1763,1764,1771],"ul",{},[1765,1766,1767,1770],"li",{},[1073,1768,1769],{},"Return it",", with added context, if this function cannot decide what to do.",[1765,1772,1773,1776],{},[1073,1774,1775],{},"Handle it"," fully, by retrying, falling back, or reporting to the user, if it can.",[395,1778,1779],{},"Log once, at the outer boundary where the program makes its final decision.",[390,1781,1783],{"id":1782},"combining-errors","Combining errors",[395,1785,1786,1787,1790],{},"When several things can fail independently, such as validating many fields, ",[399,1788,1789],{},"errors.Join"," combines them into one error:",[427,1792,1794],{"className":429,"code":1793,"language":431,"meta":432,"style":432},"err := errors.Join(errors.New(\"name is empty\"), errors.New(\"age is negative\"))\nfmt.Println(err)\n\u002F\u002F name is empty\n\u002F\u002F age is negative\n",[399,1795,1796,1847,1861,1866],{"__ignoreMap":432},[436,1797,1798,1800,1802,1804,1806,1809,1811,1814,1816,1818,1820,1822,1825,1827,1829,1831,1833,1835,1837,1839,1842,1844],{"class":438,"line":439},[436,1799,576],{"class":442},[436,1801,454],{"class":453},[436,1803,724],{"class":442},[436,1805,460],{"class":446},[436,1807,1808],{"class":463},"Join",[436,1810,467],{"class":446},[436,1812,1813],{"class":442},"errors",[436,1815,460],{"class":446},[436,1817,729],{"class":463},[436,1819,467],{"class":446},[436,1821,471],{"class":470},[436,1823,1824],{"class":474},"name is empty",[436,1826,471],{"class":470},[436,1828,560],{"class":446},[436,1830,724],{"class":442},[436,1832,460],{"class":446},[436,1834,729],{"class":463},[436,1836,467],{"class":446},[436,1838,471],{"class":470},[436,1840,1841],{"class":474},"age is negative",[436,1843,471],{"class":470},[436,1845,1846],{"class":446},"))\n",[436,1848,1849,1851,1853,1855,1857,1859],{"class":438,"line":483},[436,1850,544],{"class":442},[436,1852,460],{"class":446},[436,1854,511],{"class":463},[436,1856,467],{"class":446},[436,1858,576],{"class":442},[436,1860,480],{"class":446},[436,1862,1863],{"class":438,"line":503},[436,1864,1865],{"class":1402},"\u002F\u002F name is empty\n",[436,1867,1868],{"class":438,"line":529},[436,1869,1870],{"class":1402},"\u002F\u002F age is negative\n",[395,1872,1873,402,1875,1877],{},[399,1874,401],{},[399,1876,405],{}," look inside a joined error too.",[390,1879,1881,1883],{"id":1880},"panic-is-different",[399,1882,409],{}," is different",[395,1885,1886,1887,1889,1890,1892],{},"A ",[1073,1888,409],{}," stops the normal flow of the program, runs deferred calls, and then crashes it with a stack trace. It is not Go's way of handling expected problems. Use errors for things that can reasonably go wrong, such as bad input, a missing file, or a network timeout. Reserve ",[399,1891,409],{}," for situations that should never happen, such as a broken internal assumption, or for failures at startup where continuing makes no sense.",[427,1894,1896],{"className":429,"code":1895,"language":431,"meta":432,"style":432},"func mustPositive(n int) int {\n    if n \u003C= 0 {\n        panic(\"n must be positive\")\n    }\n    return n\n}\n",[399,1897,1898,1918,1932,1948,1952,1959],{"__ignoreMap":432},[436,1899,1900,1902,1905,1907,1910,1912,1914,1916],{"class":438,"line":439},[436,1901,662],{"class":610},[436,1903,1904],{"class":463}," mustPositive",[436,1906,467],{"class":446},[436,1908,1909],{"class":670},"n",[436,1911,1382],{"class":614},[436,1913,682],{"class":446},[436,1915,1382],{"class":614},[436,1917,500],{"class":446},[436,1919,1920,1922,1925,1928,1930],{"class":438,"line":483},[436,1921,701],{"class":486},[436,1923,1924],{"class":442}," n",[436,1926,1927],{"class":492}," \u003C=",[436,1929,710],{"class":709},[436,1931,500],{"class":446},[436,1933,1934,1937,1939,1941,1944,1946],{"class":438,"line":503},[436,1935,1936],{"class":463},"        panic",[436,1938,467],{"class":446},[436,1940,471],{"class":470},[436,1942,1943],{"class":474},"n must be positive",[436,1945,471],{"class":470},[436,1947,480],{"class":446},[436,1949,1950],{"class":438,"line":529},[436,1951,745],{"class":446},[436,1953,1954,1956],{"class":438,"line":535},[436,1955,750],{"class":486},[436,1957,1958],{"class":442}," n\n",[436,1960,1961],{"class":438,"line":541},[436,1962,538],{"class":446},[395,1964,1965,1966,1969],{},"A panic can be intercepted with ",[399,1967,1968],{},"recover"," inside a deferred function, but this is for special cases like a web server protecting itself from one bad request. Libraries should almost never panic on conditions a caller could handle.",[390,1971,1973],{"id":1972},"common-mistakes","Common mistakes",[1762,1975,1976,1985,1995,2006,2012,2022],{},[1765,1977,1978,1981,1982,1984],{},[1073,1979,1980],{},"Ignoring errors"," with ",[399,1983,919],{}," without a good reason.",[1765,1986,1987,1990,1991,402,1993,460],{},[1073,1988,1989],{},"Comparing error strings."," Use ",[399,1992,401],{},[399,1994,405],{},[1765,1996,1997,2005],{},[1073,1998,1999,2000,2002,2003],{},"Using ",[399,2001,1079],{}," instead of ",[399,2004,938],{}," and losing the original error.",[1765,2007,2008,2011],{},[1073,2009,2010],{},"Logging and returning"," the same error.",[1765,2013,2014,2017,2018,2021],{},[1073,2015,2016],{},"Capitalising or punctuating messages"," such as ",[399,2019,2020],{},"\"Failed to load.\"",", which look odd once wrapped.",[1765,2023,2024,2029],{},[1073,2025,2026,2027,460],{},"Returning a typed nil pointer as an ",[399,2028,424],{}," See the interfaces lesson.",[390,2031,2033],{"id":2032},"practice","Practice",[395,2035,2036,2037,2040,2041,2044,2045,2047],{},"Write ",[399,2038,2039],{},"LoadPort(path string) (int, error)",". It should read a file, convert its contents to an integer, and reject any port outside 1 to 65535. Add context to each failure and keep the wrapped errors. In ",[399,2042,2043],{},"main",", call it with a path that does not exist and use ",[399,2046,401],{}," to detect the missing file.",[2049,2050,2051],"accordion",{},[2052,2053,2056,2630],"accordion-item",{"icon":2054,"label":2055},"i-lucide-lightbulb","Show solution",[427,2057,2059],{"className":429,"code":2058,"language":431,"meta":432,"style":432},"package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n    \"os\"\n    \"strconv\"\n    \"strings\"\n)\n\nfunc LoadPort(path string) (int, error) {\n    data, err := os.ReadFile(path)\n    if err != nil {\n        return 0, fmt.Errorf(\"read port file %q: %w\", path, err)\n    }\n\n    text := strings.TrimSpace(string(data))\n    port, err := strconv.Atoi(text)\n    if err != nil {\n        return 0, fmt.Errorf(\"parse port %q: %w\", text, err)\n    }\n\n    if port \u003C 1 || port > 65535 {\n        return 0, fmt.Errorf(\"port %d is outside 1-65535\", port)\n    }\n    return port, nil\n}\n\nfunc main() {\n    port, err := LoadPort(\"missing.txt\")\n    if err != nil {\n        if errors.Is(err, os.ErrNotExist) {\n            fmt.Println(\"no port file, using the default port\")\n        } else {\n            fmt.Println(\"error:\", err)\n        }\n        return\n    }\n    fmt.Println(\"listening on port\", port)\n}\n",[399,2060,2061,2069,2073,2081,2092,2100,2109,2118,2127,2131,2135,2163,2185,2197,2238,2242,2247,2274,2301,2314,2355,2360,2365,2391,2426,2431,2442,2447,2452,2464,2488,2501,2529,2550,2561,2584,2590,2596,2601,2625],{"__ignoreMap":432},[436,2062,2063,2066],{"class":438,"line":439},[436,2064,2065],{"class":610},"package",[436,2067,2068],{"class":1218}," main\n",[436,2070,2071],{"class":438,"line":483},[436,2072,1131],{"emptyLinePlaceholder":1130},[436,2074,2075,2078],{"class":438,"line":503},[436,2076,2077],{"class":486},"import",[436,2079,2080],{"class":446}," (\n",[436,2082,2083,2086,2089],{"class":438,"line":529},[436,2084,2085],{"class":470},"    \"",[436,2087,1813],{"class":2088},"sdLNw",[436,2090,2091],{"class":470},"\"\n",[436,2093,2094,2096,2098],{"class":438,"line":535},[436,2095,2085],{"class":470},[436,2097,544],{"class":2088},[436,2099,2091],{"class":470},[436,2101,2102,2104,2107],{"class":438,"line":541},[436,2103,2085],{"class":470},[436,2105,2106],{"class":2088},"os",[436,2108,2091],{"class":470},[436,2110,2111,2113,2116],{"class":438,"line":1059},[436,2112,2085],{"class":470},[436,2114,2115],{"class":2088},"strconv",[436,2117,2091],{"class":470},[436,2119,2120,2122,2125],{"class":438,"line":1558},[436,2121,2085],{"class":470},[436,2123,2124],{"class":2088},"strings",[436,2126,2091],{"class":470},[436,2128,2129],{"class":438,"line":1563},[436,2130,480],{"class":446},[436,2132,2133],{"class":438,"line":1568},[436,2134,1131],{"emptyLinePlaceholder":1130},[436,2136,2137,2139,2142,2144,2146,2148,2150,2152,2155,2157,2159,2161],{"class":438,"line":1589},[436,2138,662],{"class":610},[436,2140,2141],{"class":463}," LoadPort",[436,2143,467],{"class":446},[436,2145,956],{"class":670},[436,2147,959],{"class":614},[436,2149,682],{"class":446},[436,2151,685],{"class":446},[436,2153,2154],{"class":614},"int",[436,2156,447],{"class":446},[436,2158,615],{"class":614},[436,2160,682],{"class":446},[436,2162,500],{"class":446},[436,2164,2165,2167,2169,2171,2173,2175,2177,2179,2181,2183],{"class":438,"line":1604},[436,2166,980],{"class":442},[436,2168,447],{"class":446},[436,2170,450],{"class":442},[436,2172,454],{"class":453},[436,2174,457],{"class":442},[436,2176,460],{"class":446},[436,2178,464],{"class":463},[436,2180,467],{"class":446},[436,2182,956],{"class":442},[436,2184,480],{"class":446},[436,2186,2187,2189,2191,2193,2195],{"class":438,"line":1640},[436,2188,701],{"class":486},[436,2190,450],{"class":442},[436,2192,493],{"class":492},[436,2194,497],{"class":496},[436,2196,500],{"class":446},[436,2198,2199,2201,2203,2205,2207,2209,2211,2213,2215,2218,2221,2223,2225,2227,2229,2232,2234,2236],{"class":438,"line":1645},[436,2200,717],{"class":486},[436,2202,710],{"class":709},[436,2204,447],{"class":446},[436,2206,786],{"class":442},[436,2208,460],{"class":446},[436,2210,791],{"class":463},[436,2212,467],{"class":446},[436,2214,471],{"class":470},[436,2216,2217],{"class":474},"read port file ",[436,2219,2220],{"class":801},"%q",[436,2222,1318],{"class":474},[436,2224,938],{"class":801},[436,2226,471],{"class":470},[436,2228,447],{"class":446},[436,2230,2231],{"class":442}," path",[436,2233,447],{"class":446},[436,2235,450],{"class":442},[436,2237,480],{"class":446},[436,2239,2240],{"class":438,"line":1652},[436,2241,745],{"class":446},[436,2243,2245],{"class":438,"line":2244},16,[436,2246,1131],{"emptyLinePlaceholder":1130},[436,2248,2250,2253,2255,2258,2260,2263,2265,2268,2270,2272],{"class":438,"line":2249},17,[436,2251,2252],{"class":442},"    text",[436,2254,454],{"class":453},[436,2256,2257],{"class":442}," strings",[436,2259,460],{"class":446},[436,2261,2262],{"class":463},"TrimSpace",[436,2264,467],{"class":446},[436,2266,2267],{"class":614},"string",[436,2269,467],{"class":446},[436,2271,443],{"class":442},[436,2273,1846],{"class":446},[436,2275,2277,2280,2282,2284,2286,2289,2291,2294,2296,2299],{"class":438,"line":2276},18,[436,2278,2279],{"class":442},"    port",[436,2281,447],{"class":446},[436,2283,450],{"class":442},[436,2285,454],{"class":453},[436,2287,2288],{"class":442}," strconv",[436,2290,460],{"class":446},[436,2292,2293],{"class":463},"Atoi",[436,2295,467],{"class":446},[436,2297,2298],{"class":442},"text",[436,2300,480],{"class":446},[436,2302,2304,2306,2308,2310,2312],{"class":438,"line":2303},19,[436,2305,701],{"class":486},[436,2307,450],{"class":442},[436,2309,493],{"class":492},[436,2311,497],{"class":496},[436,2313,500],{"class":446},[436,2315,2317,2319,2321,2323,2325,2327,2329,2331,2333,2336,2338,2340,2342,2344,2346,2349,2351,2353],{"class":438,"line":2316},20,[436,2318,717],{"class":486},[436,2320,710],{"class":709},[436,2322,447],{"class":446},[436,2324,786],{"class":442},[436,2326,460],{"class":446},[436,2328,791],{"class":463},[436,2330,467],{"class":446},[436,2332,471],{"class":470},[436,2334,2335],{"class":474},"parse port ",[436,2337,2220],{"class":801},[436,2339,1318],{"class":474},[436,2341,938],{"class":801},[436,2343,471],{"class":470},[436,2345,447],{"class":446},[436,2347,2348],{"class":442}," text",[436,2350,447],{"class":446},[436,2352,450],{"class":442},[436,2354,480],{"class":446},[436,2356,2358],{"class":438,"line":2357},21,[436,2359,745],{"class":446},[436,2361,2363],{"class":438,"line":2362},22,[436,2364,1131],{"emptyLinePlaceholder":1130},[436,2366,2368,2370,2373,2375,2378,2381,2383,2386,2389],{"class":438,"line":2367},23,[436,2369,701],{"class":486},[436,2371,2372],{"class":442}," port",[436,2374,1597],{"class":492},[436,2376,2377],{"class":709}," 1",[436,2379,2380],{"class":492}," ||",[436,2382,2372],{"class":442},[436,2384,2385],{"class":492}," >",[436,2387,2388],{"class":709}," 65535",[436,2390,500],{"class":446},[436,2392,2394,2396,2398,2400,2402,2404,2406,2408,2410,2413,2415,2418,2420,2422,2424],{"class":438,"line":2393},24,[436,2395,717],{"class":486},[436,2397,710],{"class":709},[436,2399,447],{"class":446},[436,2401,786],{"class":442},[436,2403,460],{"class":446},[436,2405,791],{"class":463},[436,2407,467],{"class":446},[436,2409,471],{"class":470},[436,2411,2412],{"class":474},"port ",[436,2414,802],{"class":801},[436,2416,2417],{"class":474}," is outside 1-65535",[436,2419,471],{"class":470},[436,2421,447],{"class":446},[436,2423,2372],{"class":442},[436,2425,480],{"class":446},[436,2427,2429],{"class":438,"line":2428},25,[436,2430,745],{"class":446},[436,2432,2434,2436,2438,2440],{"class":438,"line":2433},26,[436,2435,750],{"class":486},[436,2437,2372],{"class":442},[436,2439,447],{"class":446},[436,2441,763],{"class":496},[436,2443,2445],{"class":438,"line":2444},27,[436,2446,538],{"class":446},[436,2448,2450],{"class":438,"line":2449},28,[436,2451,1131],{"emptyLinePlaceholder":1130},[436,2453,2455,2457,2460,2462],{"class":438,"line":2454},29,[436,2456,662],{"class":610},[436,2458,2459],{"class":463}," main",[436,2461,627],{"class":446},[436,2463,500],{"class":446},[436,2465,2467,2469,2471,2473,2475,2477,2479,2481,2484,2486],{"class":438,"line":2466},30,[436,2468,2279],{"class":442},[436,2470,447],{"class":446},[436,2472,450],{"class":442},[436,2474,454],{"class":453},[436,2476,2141],{"class":463},[436,2478,467],{"class":446},[436,2480,471],{"class":470},[436,2482,2483],{"class":474},"missing.txt",[436,2485,471],{"class":470},[436,2487,480],{"class":446},[436,2489,2491,2493,2495,2497,2499],{"class":438,"line":2490},31,[436,2492,701],{"class":486},[436,2494,450],{"class":442},[436,2496,493],{"class":492},[436,2498,497],{"class":496},[436,2500,500],{"class":446},[436,2502,2504,2507,2509,2511,2513,2515,2517,2519,2521,2523,2525,2527],{"class":438,"line":2503},32,[436,2505,2506],{"class":486},"        if",[436,2508,724],{"class":442},[436,2510,460],{"class":446},[436,2512,1142],{"class":463},[436,2514,467],{"class":446},[436,2516,576],{"class":442},[436,2518,447],{"class":446},[436,2520,457],{"class":442},[436,2522,460],{"class":446},[436,2524,1155],{"class":442},[436,2526,682],{"class":446},[436,2528,500],{"class":446},[436,2530,2532,2535,2537,2539,2541,2543,2546,2548],{"class":438,"line":2531},33,[436,2533,2534],{"class":442},"            fmt",[436,2536,460],{"class":446},[436,2538,511],{"class":463},[436,2540,467],{"class":446},[436,2542,471],{"class":470},[436,2544,2545],{"class":474},"no port file, using the default port",[436,2547,471],{"class":470},[436,2549,480],{"class":446},[436,2551,2553,2556,2559],{"class":438,"line":2552},34,[436,2554,2555],{"class":446},"        }",[436,2557,2558],{"class":486}," else",[436,2560,500],{"class":446},[436,2562,2564,2566,2568,2570,2572,2574,2576,2578,2580,2582],{"class":438,"line":2563},35,[436,2565,2534],{"class":442},[436,2567,460],{"class":446},[436,2569,511],{"class":463},[436,2571,467],{"class":446},[436,2573,471],{"class":470},[436,2575,883],{"class":474},[436,2577,471],{"class":470},[436,2579,447],{"class":446},[436,2581,450],{"class":442},[436,2583,480],{"class":446},[436,2585,2587],{"class":438,"line":2586},36,[436,2588,2589],{"class":446},"        }\n",[436,2591,2593],{"class":438,"line":2592},37,[436,2594,2595],{"class":486},"        return\n",[436,2597,2599],{"class":438,"line":2598},38,[436,2600,745],{"class":446},[436,2602,2604,2606,2608,2610,2612,2614,2617,2619,2621,2623],{"class":438,"line":2603},39,[436,2605,506],{"class":442},[436,2607,460],{"class":446},[436,2609,511],{"class":463},[436,2611,467],{"class":446},[436,2613,471],{"class":470},[436,2615,2616],{"class":474},"listening on port",[436,2618,471],{"class":470},[436,2620,447],{"class":446},[436,2622,2372],{"class":442},[436,2624,480],{"class":446},[436,2626,2628],{"class":438,"line":2627},40,[436,2629,538],{"class":446},[395,2631,2632,2633,2635,2636,2638,2639,2641,2642,2645,2646,2648,2649,2651],{},"Running this without a ",[399,2634,2483],{}," file prints ",[399,2637,2545],{},". Each failure point adds its own context, and the first two use ",[399,2640,938],{},", so the underlying cause, such as ",[399,2643,2644],{},"os.ErrNotExist"," or a ",[399,2647,2115],{}," error, is still reachable. The range check has no underlying cause to wrap, so it uses a plain message. ",[399,2650,2043],{}," makes the final decision, which is the right place to inspect the error.",[390,2653,2655],{"id":2654},"summary","Summary",[395,2657,2658,2659,2661,2662,2664,2665,2667,2668,402,2670,2672,2673,2675,2676,2678,2679,2681],{},"In Go, an error is an ordinary value, returned as the last result and checked immediately with ",[399,2660,587],{},". The ",[399,2663,424],{}," interface needs only an ",[399,2666,640],{}," method. Add context as errors travel upward with ",[399,2669,773],{},[399,2671,938],{},", and inspect them with ",[399,2674,401],{}," for known values and ",[399,2677,405],{}," for types, never by comparing text. Each layer should either handle an error or return it, and ",[399,2680,409],{}," is for situations that should never happen, not for ordinary failures.",[395,2683,2684,2685,402,2691,460],{},"Official reference: ",[671,2686,2690],{"href":2687,"rel":2688},"https:\u002F\u002Fgo.dev\u002Fblog\u002Fgo1.13-errors",[2689],"nofollow","Working with errors in Go 1.13",[671,2692,2695],{"href":2693,"rel":2694},"https:\u002F\u002Fgo.dev\u002Fdoc\u002Feffective_go#errors",[2689],"Effective Go: Errors",[2697,2698],"learning-lesson-complete",{},[2700,2701],"learning-lesson-navigation",{"next":66,"nextTitle":65,"previous":56,"previousTitle":55},[2703,2704,2705],"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 .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}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 .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 .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 .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .sp8Z5, html code.shiki .sp8Z5{--shiki-light:#B58900;--shiki-default:#89DDFF;--shiki-dark:#D19A66}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 .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 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 .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}html pre.shiki code .st3jj, html code.shiki .st3jj{--shiki-light:#CB4B16;--shiki-default:#F07178;--shiki-dark:#D19A66}html pre.shiki code .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}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 .sdLNw, html code.shiki .sdLNw{--shiki-light:#2AA198;--shiki-default:#FFCB6B;--shiki-dark:#98C379}",{"title":432,"searchDepth":483,"depth":483,"links":2707},[2708,2709,2710,2712,2713,2714,2715,2716,2717,2718,2719,2720,2722,2723,2724],{"id":392,"depth":483,"text":393},{"id":413,"depth":483,"text":414},{"id":591,"depth":483,"text":2711},"The error interface",{"id":644,"depth":483,"text":645},{"id":823,"depth":483,"text":824},{"id":927,"depth":483,"text":928},{"id":1086,"depth":483,"text":1087},{"id":1324,"depth":483,"text":1325},{"id":1458,"depth":483,"text":1459},{"id":1756,"depth":483,"text":1757},{"id":1782,"depth":483,"text":1783},{"id":1880,"depth":483,"text":2721},"panic is different",{"id":1972,"depth":483,"text":1973},{"id":2032,"depth":483,"text":2033},{"id":2654,"depth":483,"text":2655},"Return, wrap, inspect, and design errors so that failures are visible and useful.","md",null,{},{"icon":63},{"description":2731,"title":60},"Learn idiomatic Go error handling with the error interface, errors.New, fmt.Errorf and %w wrapping, errors.Is and errors.As, sentinel and custom errors, and when to use panic.","2dtHdUXRkXacxPccM5KV8IsFwP9qh6P3GMFpdAQUbwQ",[2734,2736],{"title":55,"path":56,"stem":57,"description":2735,"icon":58,"children":-1},"Describe behaviour with small interfaces, understand implicit implementation, and avoid the typed-nil trap.",{"title":65,"path":66,"stem":67,"description":2737,"icon":68,"children":-1},"Give values domain meaning with defined types, build enum-like constants with iota, and learn how an alias differs.",1794118097362]