[{"data":1,"prerenderedAt":2554},["ShallowReactive",2],{"navigation_docs":3,"-fundamentals-structs-and-methods":377,"-fundamentals-structs-and-methods-surround":2549},[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":50,"body":379,"description":2541,"extension":2542,"links":2543,"meta":2544,"navigation":2545,"path":51,"seo":2546,"stem":52,"__hash__":2548},"docs\u002F2.fundamentals\u002F10.structs-and-methods.md",{"type":380,"value":381,"toc":2525},"minimark",[382,389,394,407,411,476,490,618,625,629,632,717,720,724,731,823,837,841,855,1047,1063,1067,1074,1120,1126,1171,1182,1226,1232,1236,1242,1314,1321,1325,1335,1400,1410,1509,1513,1520,1782,1813,1817,1824,1828,1838,1842,1878,1882,1908,2494,2498,2501,2515,2518,2521],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Group related data into structs, give them behaviour with methods, and choose between value and pointer receivers.","Beginner","Pointers, Functions, Variables and Data Types","40 minutes",[390,391,393],"h2",{"id":392},"what-you-will-learn","What you will learn",[395,396,397,398,402,403,406],"p",{},"Real programs deal with things like users, orders, and servers, each made of several related pieces of data. A ",[399,400,401],"strong",{},"struct"," groups those pieces into one type, and a ",[399,404,405],{},"method"," attaches behaviour to it. This lesson covers declaring and creating structs, writing methods, the important choice between value and pointer receivers, constructor functions, and embedding, which is how Go reuses code without inheritance.",[390,408,410],{"id":409},"defining-a-struct","Defining a struct",[412,413,418],"pre",{"className":414,"code":415,"language":416,"meta":417,"style":417},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","type User struct {\n    ID     int\n    Name   string\n    Active bool\n}\n","go","",[419,420,421,441,452,461,470],"code",{"__ignoreMap":417},[422,423,426,430,434,437],"span",{"class":424,"line":425},"line",1,[422,427,429],{"class":428},"sEOdo","type",[422,431,433],{"class":432},"snwKR"," User",[422,435,436],{"class":428}," struct",[422,438,440],{"class":439},"shFME"," {\n",[422,442,444,448],{"class":424,"line":443},2,[422,445,447],{"class":446},"s45-6","    ID",[422,449,451],{"class":450},"sF6sA","     int\n",[422,453,455,458],{"class":424,"line":454},3,[422,456,457],{"class":446},"    Name",[422,459,460],{"class":450},"   string\n",[422,462,464,467],{"class":424,"line":463},4,[422,465,466],{"class":446},"    Active",[422,468,469],{"class":450}," bool\n",[422,471,473],{"class":424,"line":472},5,[422,474,475],{"class":439},"}\n",[395,477,478,481,482,485,486,489],{},[419,479,480],{},"type User struct { ... }"," creates a new type named ",[419,483,484],{},"User"," with three ",[399,487,488],{},"fields",". Each field has a name and a type. Create a value with a literal, naming each field:",[412,491,493],{"className":414,"code":492,"language":416,"meta":417,"style":417},"user := User{\n    ID:     1,\n    Name:   \"Mira\",\n    Active: true,\n}\n\nfmt.Println(user.Name) \u002F\u002F Mira\nuser.Name = \"Mira K.\"  \u002F\u002F fields can be changed\n",[419,494,495,509,523,542,554,558,565,595],{"__ignoreMap":417},[422,496,497,500,504,506],{"class":424,"line":425},[422,498,499],{"class":446},"user",[422,501,503],{"class":502},"su2KV"," :=",[422,505,433],{"class":432},[422,507,508],{"class":439},"{\n",[422,510,511,513,516,520],{"class":424,"line":443},[422,512,447],{"class":446},[422,514,515],{"class":439},":",[422,517,519],{"class":518},"sAJzr","     1",[422,521,522],{"class":439},",\n",[422,524,525,527,529,533,537,540],{"class":424,"line":454},[422,526,457],{"class":446},[422,528,515],{"class":439},[422,530,532],{"class":531},"sED5a","   \"",[422,534,536],{"class":535},"sVazx","Mira",[422,538,539],{"class":531},"\"",[422,541,522],{"class":439},[422,543,544,546,548,552],{"class":424,"line":463},[422,545,466],{"class":446},[422,547,515],{"class":439},[422,549,551],{"class":550},"sqQ1y"," true",[422,553,522],{"class":439},[422,555,556],{"class":424,"line":472},[422,557,475],{"class":439},[422,559,561],{"class":424,"line":560},6,[422,562,564],{"emptyLinePlaceholder":563},true,"\n",[422,566,568,571,574,578,581,583,585,588,591],{"class":424,"line":567},7,[422,569,570],{"class":446},"fmt",[422,572,573],{"class":439},".",[422,575,577],{"class":576},"sisdC","Println",[422,579,580],{"class":439},"(",[422,582,499],{"class":446},[422,584,573],{"class":439},[422,586,587],{"class":446},"Name",[422,589,590],{"class":439},")",[422,592,594],{"class":593},"sJsHy"," \u002F\u002F Mira\n",[422,596,598,600,602,604,607,610,613,615],{"class":424,"line":597},8,[422,599,499],{"class":446},[422,601,573],{"class":439},[422,603,587],{"class":446},[422,605,606],{"class":502}," =",[422,608,609],{"class":531}," \"",[422,611,612],{"class":535},"Mira K.",[422,614,539],{"class":531},[422,616,617],{"class":593},"  \u002F\u002F fields can be changed\n",[395,619,620,621,624],{},"Use the dot to read or write a field. Always name the fields in the literal. The shorter positional form, ",[419,622,623],{},"User{1, \"Mira\", true}",", breaks as soon as someone adds or reorders a field, and it is hard to read.",[390,626,628],{"id":627},"zero-values","Zero values",[395,630,631],{},"If you leave fields out, they start at their zero values:",[412,633,635],{"className":414,"code":634,"language":416,"meta":417,"style":417},"var u User\nfmt.Println(u) \u002F\u002F {0  false}\n\npartial := User{Name: \"Sam\"}\nfmt.Println(partial.ID) \u002F\u002F 0\n",[419,636,637,648,666,670,695],{"__ignoreMap":417},[422,638,639,642,645],{"class":424,"line":425},[422,640,641],{"class":428},"var",[422,643,644],{"class":446}," u",[422,646,647],{"class":432}," User\n",[422,649,650,652,654,656,658,661,663],{"class":424,"line":443},[422,651,570],{"class":446},[422,653,573],{"class":439},[422,655,577],{"class":576},[422,657,580],{"class":439},[422,659,660],{"class":446},"u",[422,662,590],{"class":439},[422,664,665],{"class":593}," \u002F\u002F {0  false}\n",[422,667,668],{"class":424,"line":454},[422,669,564],{"emptyLinePlaceholder":563},[422,671,672,675,677,679,682,684,686,688,691,693],{"class":424,"line":463},[422,673,674],{"class":446},"partial",[422,676,503],{"class":502},[422,678,433],{"class":432},[422,680,681],{"class":439},"{",[422,683,587],{"class":446},[422,685,515],{"class":439},[422,687,609],{"class":531},[422,689,690],{"class":535},"Sam",[422,692,539],{"class":531},[422,694,475],{"class":439},[422,696,697,699,701,703,705,707,709,712,714],{"class":424,"line":472},[422,698,570],{"class":446},[422,700,573],{"class":439},[422,702,577],{"class":576},[422,704,580],{"class":439},[422,706,674],{"class":446},[422,708,573],{"class":439},[422,710,711],{"class":446},"ID",[422,713,590],{"class":439},[422,715,716],{"class":593}," \u002F\u002F 0\n",[395,718,719],{},"Good Go types are designed so that this zero state is already usable, which saves you from needing setup code everywhere.",[390,721,723],{"id":722},"structs-are-copied","Structs are copied",[395,725,726,727,730],{},"A struct is a value, just like an ",[419,728,729],{},"int",". Assigning it or passing it to a function copies all its fields:",[412,732,734],{"className":414,"code":733,"language":416,"meta":417,"style":417},"a := User{Name: \"Ada\"}\nb := a\nb.Name = \"Bea\"\n\nfmt.Println(a.Name, b.Name) \u002F\u002F Ada Bea\n",[419,735,736,760,770,788,792],{"__ignoreMap":417},[422,737,738,741,743,745,747,749,751,753,756,758],{"class":424,"line":425},[422,739,740],{"class":446},"a",[422,742,503],{"class":502},[422,744,433],{"class":432},[422,746,681],{"class":439},[422,748,587],{"class":446},[422,750,515],{"class":439},[422,752,609],{"class":531},[422,754,755],{"class":535},"Ada",[422,757,539],{"class":531},[422,759,475],{"class":439},[422,761,762,765,767],{"class":424,"line":443},[422,763,764],{"class":446},"b",[422,766,503],{"class":502},[422,768,769],{"class":446}," a\n",[422,771,772,774,776,778,780,782,785],{"class":424,"line":454},[422,773,764],{"class":446},[422,775,573],{"class":439},[422,777,587],{"class":446},[422,779,606],{"class":502},[422,781,609],{"class":531},[422,783,784],{"class":535},"Bea",[422,786,787],{"class":531},"\"\n",[422,789,790],{"class":424,"line":463},[422,791,564],{"emptyLinePlaceholder":563},[422,793,794,796,798,800,802,804,806,808,811,814,816,818,820],{"class":424,"line":472},[422,795,570],{"class":446},[422,797,573],{"class":439},[422,799,577],{"class":576},[422,801,580],{"class":439},[422,803,740],{"class":446},[422,805,573],{"class":439},[422,807,587],{"class":446},[422,809,810],{"class":439},",",[422,812,813],{"class":446}," b",[422,815,573],{"class":439},[422,817,587],{"class":446},[422,819,590],{"class":439},[422,821,822],{"class":593}," \u002F\u002F Ada Bea\n",[395,824,825,826,829,830,833,834,573],{},"To share one struct instead, use a pointer: ",[419,827,828],{},"p := &a",". Go lets you write ",[419,831,832],{},"p.Name"," instead of ",[419,835,836],{},"(*p).Name",[390,838,840],{"id":839},"adding-methods","Adding methods",[395,842,843,844,846,847,850,851,854],{},"A ",[399,845,405],{}," is a function with a ",[399,848,849],{},"receiver",", written between ",[419,852,853],{},"func"," and the method name:",[412,856,858],{"className":414,"code":857,"language":416,"meta":417,"style":417},"func (u User) DisplayName() string {\n    if u.Name == \"\" {\n        return \"Anonymous\"\n    }\n    return u.Name\n}\n\nfunc main() {\n    user := User{Name: \"Mira\"}\n    fmt.Println(user.DisplayName()) \u002F\u002F Mira\n\n    var empty User\n    fmt.Println(empty.DisplayName()) \u002F\u002F Anonymous\n}\n",[419,859,860,886,907,919,924,936,940,944,955,979,1003,1008,1019,1042],{"__ignoreMap":417},[422,861,862,864,867,871,873,875,878,881,884],{"class":424,"line":425},[422,863,853],{"class":428},[422,865,866],{"class":439}," (",[422,868,870],{"class":869},"syre3","u ",[422,872,484],{"class":432},[422,874,590],{"class":439},[422,876,877],{"class":576}," DisplayName",[422,879,880],{"class":439},"()",[422,882,883],{"class":450}," string",[422,885,440],{"class":439},[422,887,888,892,894,896,898,902,905],{"class":424,"line":443},[422,889,891],{"class":890},"s7Bcg","    if",[422,893,644],{"class":446},[422,895,573],{"class":439},[422,897,587],{"class":446},[422,899,901],{"class":900},"sRBoR"," ==",[422,903,904],{"class":531}," \"\"",[422,906,440],{"class":439},[422,908,909,912,914,917],{"class":424,"line":454},[422,910,911],{"class":890},"        return",[422,913,609],{"class":531},[422,915,916],{"class":535},"Anonymous",[422,918,787],{"class":531},[422,920,921],{"class":424,"line":463},[422,922,923],{"class":439},"    }\n",[422,925,926,929,931,933],{"class":424,"line":472},[422,927,928],{"class":890},"    return",[422,930,644],{"class":446},[422,932,573],{"class":439},[422,934,935],{"class":446},"Name\n",[422,937,938],{"class":424,"line":560},[422,939,475],{"class":439},[422,941,942],{"class":424,"line":567},[422,943,564],{"emptyLinePlaceholder":563},[422,945,946,948,951,953],{"class":424,"line":597},[422,947,853],{"class":428},[422,949,950],{"class":576}," main",[422,952,880],{"class":439},[422,954,440],{"class":439},[422,956,958,961,963,965,967,969,971,973,975,977],{"class":424,"line":957},9,[422,959,960],{"class":446},"    user",[422,962,503],{"class":502},[422,964,433],{"class":432},[422,966,681],{"class":439},[422,968,587],{"class":446},[422,970,515],{"class":439},[422,972,609],{"class":531},[422,974,536],{"class":535},[422,976,539],{"class":531},[422,978,475],{"class":439},[422,980,982,985,987,989,991,993,995,998,1001],{"class":424,"line":981},10,[422,983,984],{"class":446},"    fmt",[422,986,573],{"class":439},[422,988,577],{"class":576},[422,990,580],{"class":439},[422,992,499],{"class":446},[422,994,573],{"class":439},[422,996,997],{"class":576},"DisplayName",[422,999,1000],{"class":439},"())",[422,1002,594],{"class":593},[422,1004,1006],{"class":424,"line":1005},11,[422,1007,564],{"emptyLinePlaceholder":563},[422,1009,1011,1014,1017],{"class":424,"line":1010},12,[422,1012,1013],{"class":428},"    var",[422,1015,1016],{"class":446}," empty",[422,1018,647],{"class":432},[422,1020,1022,1024,1026,1028,1030,1033,1035,1037,1039],{"class":424,"line":1021},13,[422,1023,984],{"class":446},[422,1025,573],{"class":439},[422,1027,577],{"class":576},[422,1029,580],{"class":439},[422,1031,1032],{"class":446},"empty",[422,1034,573],{"class":439},[422,1036,997],{"class":576},[422,1038,1000],{"class":439},[422,1040,1041],{"class":593}," \u002F\u002F Anonymous\n",[422,1043,1045],{"class":424,"line":1044},14,[422,1046,475],{"class":439},[395,1048,1049,1052,1053,1055,1056,1058,1059,1062],{},[419,1050,1051],{},"(u User)"," says \"this function belongs to ",[419,1054,484],{},", and inside it the value is called ",[419,1057,660],{},"\". You call it with the dot, as ",[419,1060,1061],{},"user.DisplayName()",". Methods can be added to any type you define in the same package, not only structs.",[390,1064,1066],{"id":1065},"value-or-pointer-receiver","Value or pointer receiver?",[395,1068,1069,1070,1073],{},"This is the decision you will make most often with methods. A ",[399,1071,1072],{},"value receiver"," gets a copy of the struct:",[412,1075,1077],{"className":414,"code":1076,"language":416,"meta":417,"style":417},"func (u User) Deactivate() {\n    u.Active = false \u002F\u002F changes only the copy\n}\n",[419,1078,1079,1098,1116],{"__ignoreMap":417},[422,1080,1081,1083,1085,1087,1089,1091,1094,1096],{"class":424,"line":425},[422,1082,853],{"class":428},[422,1084,866],{"class":439},[422,1086,870],{"class":869},[422,1088,484],{"class":432},[422,1090,590],{"class":439},[422,1092,1093],{"class":576}," Deactivate",[422,1095,880],{"class":439},[422,1097,440],{"class":439},[422,1099,1100,1103,1105,1108,1110,1113],{"class":424,"line":443},[422,1101,1102],{"class":446},"    u",[422,1104,573],{"class":439},[422,1106,1107],{"class":446},"Active",[422,1109,606],{"class":502},[422,1111,1112],{"class":550}," false",[422,1114,1115],{"class":593}," \u002F\u002F changes only the copy\n",[422,1117,1118],{"class":424,"line":454},[422,1119,475],{"class":439},[395,1121,843,1122,1125],{},[399,1123,1124],{},"pointer receiver"," gets the original:",[412,1127,1129],{"className":414,"code":1128,"language":416,"meta":417,"style":417},"func (u *User) Deactivate() {\n    u.Active = false \u002F\u002F changes the real user\n}\n",[419,1130,1131,1152,1167],{"__ignoreMap":417},[422,1132,1133,1135,1137,1139,1142,1144,1146,1148,1150],{"class":424,"line":425},[422,1134,853],{"class":428},[422,1136,866],{"class":439},[422,1138,870],{"class":869},[422,1140,1141],{"class":428},"*",[422,1143,484],{"class":432},[422,1145,590],{"class":439},[422,1147,1093],{"class":576},[422,1149,880],{"class":439},[422,1151,440],{"class":439},[422,1153,1154,1156,1158,1160,1162,1164],{"class":424,"line":443},[422,1155,1102],{"class":446},[422,1157,573],{"class":439},[422,1159,1107],{"class":446},[422,1161,606],{"class":502},[422,1163,1112],{"class":550},[422,1165,1166],{"class":593}," \u002F\u002F changes the real user\n",[422,1168,1169],{"class":424,"line":454},[422,1170,475],{"class":439},[395,1172,1173,1174,1177,1178,1181],{},"Calling it looks the same either way. With a pointer receiver, ",[419,1175,1176],{},"user.Deactivate()"," works on a normal variable because Go passes ",[419,1179,1180],{},"&user"," for you.",[1183,1184,1185,1198],"table",{},[1186,1187,1188],"thead",{},[1189,1190,1191,1195],"tr",{},[1192,1193,1194],"th",{},"Use a pointer receiver when",[1192,1196,1197],{},"Use a value receiver when",[1199,1200,1201,1210,1218],"tbody",{},[1189,1202,1203,1207],{},[1204,1205,1206],"td",{},"The method changes the struct",[1204,1208,1209],{},"The method only reads the struct",[1189,1211,1212,1215],{},[1204,1213,1214],{},"The struct is large and copying is wasteful",[1204,1216,1217],{},"The struct is small",[1189,1219,1220,1223],{},[1204,1221,1222],{},"The struct holds something that must not be copied, like a mutex",[1204,1224,1225],{},"The type behaves like a plain value, such as a coordinate",[395,1227,1228,1231],{},[399,1229,1230],{},"Be consistent."," If any method of a type needs a pointer receiver, make all of them pointer receivers. Mixing the two makes the rules for interfaces, covered in the next lesson, harder to follow.",[390,1233,1235],{"id":1234},"constructors-are-plain-functions","Constructors are plain functions",[395,1237,1238,1239,515],{},"Go has no special constructor syntax. A function that returns a ready-to-use value is the convention, usually named ",[419,1240,1241],{},"NewTypeName",[412,1243,1245],{"className":414,"code":1244,"language":416,"meta":417,"style":417},"func NewUser(id int, name string) User {\n    return User{ID: id, Name: name, Active: true}\n}\n",[419,1246,1247,1275,1310],{"__ignoreMap":417},[422,1248,1249,1251,1254,1256,1259,1262,1264,1267,1269,1271,1273],{"class":424,"line":425},[422,1250,853],{"class":428},[422,1252,1253],{"class":576}," NewUser",[422,1255,580],{"class":439},[422,1257,1258],{"class":869},"id",[422,1260,1261],{"class":450}," int",[422,1263,810],{"class":439},[422,1265,1266],{"class":869}," name",[422,1268,883],{"class":450},[422,1270,590],{"class":439},[422,1272,433],{"class":432},[422,1274,440],{"class":439},[422,1276,1277,1279,1281,1283,1285,1287,1290,1292,1295,1297,1299,1301,1304,1306,1308],{"class":424,"line":443},[422,1278,928],{"class":890},[422,1280,433],{"class":432},[422,1282,681],{"class":439},[422,1284,711],{"class":446},[422,1286,515],{"class":439},[422,1288,1289],{"class":446}," id",[422,1291,810],{"class":439},[422,1293,1294],{"class":446}," Name",[422,1296,515],{"class":439},[422,1298,1266],{"class":446},[422,1300,810],{"class":439},[422,1302,1303],{"class":446}," Active",[422,1305,515],{"class":439},[422,1307,551],{"class":550},[422,1309,475],{"class":439},[422,1311,1312],{"class":424,"line":454},[422,1313,475],{"class":439},[395,1315,1316,1317,1320],{},"Constructors are worth writing when there are defaults to apply or inputs to check. For simple data, a literal is fine. Many constructors return a pointer, ",[419,1318,1319],{},"*User",", when the type has pointer receivers.",[390,1322,1324],{"id":1323},"printing-a-struct","Printing a struct",[395,1326,1327,1330,1331,1334],{},[419,1328,1329],{},"fmt.Println(user)"," prints the fields in braces. Use ",[419,1332,1333],{},"%+v"," to include the field names:",[412,1336,1338],{"className":414,"code":1337,"language":416,"meta":417,"style":417},"fmt.Printf(\"%v\\n\", user)  \u002F\u002F {1 Mira true}\nfmt.Printf(\"%+v\\n\", user) \u002F\u002F {ID:1 Name:Mira Active:true}\n",[419,1339,1340,1373],{"__ignoreMap":417},[422,1341,1342,1344,1346,1349,1351,1353,1357,1361,1363,1365,1368,1370],{"class":424,"line":425},[422,1343,570],{"class":446},[422,1345,573],{"class":439},[422,1347,1348],{"class":576},"Printf",[422,1350,580],{"class":439},[422,1352,539],{"class":531},[422,1354,1356],{"class":1355},"st3jj","%v",[422,1358,1360],{"class":1359},"sk51v","\\n",[422,1362,539],{"class":531},[422,1364,810],{"class":439},[422,1366,1367],{"class":446}," user",[422,1369,590],{"class":439},[422,1371,1372],{"class":593},"  \u002F\u002F {1 Mira true}\n",[422,1374,1375,1377,1379,1381,1383,1385,1387,1389,1391,1393,1395,1397],{"class":424,"line":443},[422,1376,570],{"class":446},[422,1378,573],{"class":439},[422,1380,1348],{"class":576},[422,1382,580],{"class":439},[422,1384,539],{"class":531},[422,1386,1333],{"class":1355},[422,1388,1360],{"class":1359},[422,1390,539],{"class":531},[422,1392,810],{"class":439},[422,1394,1367],{"class":446},[422,1396,590],{"class":439},[422,1398,1399],{"class":593}," \u002F\u002F {ID:1 Name:Mira Active:true}\n",[395,1401,1402,1403,1406,1407,1409],{},"To control the output yourself, give the type a ",[419,1404,1405],{},"String"," method. The ",[419,1408,570],{}," package calls it automatically:",[412,1411,1413],{"className":414,"code":1412,"language":416,"meta":417,"style":417},"func (u User) String() string {\n    return fmt.Sprintf(\"%s (#%d)\", u.Name, u.ID)\n}\n\nfmt.Println(user) \u002F\u002F Mira (#1)\n",[419,1414,1415,1436,1484,1488,1492],{"__ignoreMap":417},[422,1416,1417,1419,1421,1423,1425,1427,1430,1432,1434],{"class":424,"line":425},[422,1418,853],{"class":428},[422,1420,866],{"class":439},[422,1422,870],{"class":869},[422,1424,484],{"class":432},[422,1426,590],{"class":439},[422,1428,1429],{"class":576}," String",[422,1431,880],{"class":439},[422,1433,883],{"class":450},[422,1435,440],{"class":439},[422,1437,1438,1440,1443,1445,1448,1450,1452,1455,1458,1461,1463,1465,1467,1469,1471,1473,1475,1477,1479,1481],{"class":424,"line":443},[422,1439,928],{"class":890},[422,1441,1442],{"class":446}," fmt",[422,1444,573],{"class":439},[422,1446,1447],{"class":576},"Sprintf",[422,1449,580],{"class":439},[422,1451,539],{"class":531},[422,1453,1454],{"class":1355},"%s",[422,1456,1457],{"class":535}," (#",[422,1459,1460],{"class":1355},"%d",[422,1462,590],{"class":535},[422,1464,539],{"class":531},[422,1466,810],{"class":439},[422,1468,644],{"class":446},[422,1470,573],{"class":439},[422,1472,587],{"class":446},[422,1474,810],{"class":439},[422,1476,644],{"class":446},[422,1478,573],{"class":439},[422,1480,711],{"class":446},[422,1482,1483],{"class":439},")\n",[422,1485,1486],{"class":424,"line":454},[422,1487,475],{"class":439},[422,1489,1490],{"class":424,"line":463},[422,1491,564],{"emptyLinePlaceholder":563},[422,1493,1494,1496,1498,1500,1502,1504,1506],{"class":424,"line":472},[422,1495,570],{"class":446},[422,1497,573],{"class":439},[422,1499,577],{"class":576},[422,1501,580],{"class":439},[422,1503,499],{"class":446},[422,1505,590],{"class":439},[422,1507,1508],{"class":593}," \u002F\u002F Mira (#1)\n",[390,1510,1512],{"id":1511},"embedding-composition-not-inheritance","Embedding: composition, not inheritance",[395,1514,1515,1516,1519],{},"Go has no classes or inheritance. To reuse a type inside another, you ",[399,1517,1518],{},"embed"," it by listing its type without a field name:",[412,1521,1523],{"className":414,"code":1522,"language":416,"meta":417,"style":417},"type Contact struct {\n    Email string\n}\n\nfunc (c Contact) Domain() string {\n    return strings.Split(c.Email, \"@\")[1]\n}\n\ntype Customer struct {\n    Contact \u002F\u002F embedded\n    Name    string\n}\n\nfunc main() {\n    c := Customer{\n        Contact: Contact{Email: \"mira@example.com\"},\n        Name:    \"Mira\",\n    }\n\n    fmt.Println(c.Email)    \u002F\u002F promoted field\n    fmt.Println(c.Domain()) \u002F\u002F promoted method: example.com\n}\n",[419,1524,1525,1536,1544,1548,1552,1575,1615,1619,1623,1634,1642,1649,1653,1657,1667,1679,1705,1722,1727,1732,1754,1777],{"__ignoreMap":417},[422,1526,1527,1529,1532,1534],{"class":424,"line":425},[422,1528,429],{"class":428},[422,1530,1531],{"class":432}," Contact",[422,1533,436],{"class":428},[422,1535,440],{"class":439},[422,1537,1538,1541],{"class":424,"line":443},[422,1539,1540],{"class":446},"    Email",[422,1542,1543],{"class":450}," string\n",[422,1545,1546],{"class":424,"line":454},[422,1547,475],{"class":439},[422,1549,1550],{"class":424,"line":463},[422,1551,564],{"emptyLinePlaceholder":563},[422,1553,1554,1556,1558,1561,1564,1566,1569,1571,1573],{"class":424,"line":472},[422,1555,853],{"class":428},[422,1557,866],{"class":439},[422,1559,1560],{"class":869},"c ",[422,1562,1563],{"class":432},"Contact",[422,1565,590],{"class":439},[422,1567,1568],{"class":576}," Domain",[422,1570,880],{"class":439},[422,1572,883],{"class":450},[422,1574,440],{"class":439},[422,1576,1577,1579,1582,1584,1587,1589,1592,1594,1597,1599,1601,1604,1606,1609,1612],{"class":424,"line":560},[422,1578,928],{"class":890},[422,1580,1581],{"class":446}," strings",[422,1583,573],{"class":439},[422,1585,1586],{"class":576},"Split",[422,1588,580],{"class":439},[422,1590,1591],{"class":446},"c",[422,1593,573],{"class":439},[422,1595,1596],{"class":446},"Email",[422,1598,810],{"class":439},[422,1600,609],{"class":531},[422,1602,1603],{"class":535},"@",[422,1605,539],{"class":531},[422,1607,1608],{"class":439},")[",[422,1610,1611],{"class":518},"1",[422,1613,1614],{"class":439},"]\n",[422,1616,1617],{"class":424,"line":567},[422,1618,475],{"class":439},[422,1620,1621],{"class":424,"line":597},[422,1622,564],{"emptyLinePlaceholder":563},[422,1624,1625,1627,1630,1632],{"class":424,"line":957},[422,1626,429],{"class":428},[422,1628,1629],{"class":432}," Customer",[422,1631,436],{"class":428},[422,1633,440],{"class":439},[422,1635,1636,1639],{"class":424,"line":981},[422,1637,1638],{"class":432},"    Contact",[422,1640,1641],{"class":593}," \u002F\u002F embedded\n",[422,1643,1644,1646],{"class":424,"line":1005},[422,1645,457],{"class":446},[422,1647,1648],{"class":450},"    string\n",[422,1650,1651],{"class":424,"line":1010},[422,1652,475],{"class":439},[422,1654,1655],{"class":424,"line":1021},[422,1656,564],{"emptyLinePlaceholder":563},[422,1658,1659,1661,1663,1665],{"class":424,"line":1044},[422,1660,853],{"class":428},[422,1662,950],{"class":576},[422,1664,880],{"class":439},[422,1666,440],{"class":439},[422,1668,1670,1673,1675,1677],{"class":424,"line":1669},15,[422,1671,1672],{"class":446},"    c",[422,1674,503],{"class":502},[422,1676,1629],{"class":432},[422,1678,508],{"class":439},[422,1680,1682,1685,1687,1689,1691,1693,1695,1697,1700,1702],{"class":424,"line":1681},16,[422,1683,1684],{"class":446},"        Contact",[422,1686,515],{"class":439},[422,1688,1531],{"class":432},[422,1690,681],{"class":439},[422,1692,1596],{"class":446},[422,1694,515],{"class":439},[422,1696,609],{"class":531},[422,1698,1699],{"class":535},"mira@example.com",[422,1701,539],{"class":531},[422,1703,1704],{"class":439},"},\n",[422,1706,1708,1711,1713,1716,1718,1720],{"class":424,"line":1707},17,[422,1709,1710],{"class":446},"        Name",[422,1712,515],{"class":439},[422,1714,1715],{"class":531},"    \"",[422,1717,536],{"class":535},[422,1719,539],{"class":531},[422,1721,522],{"class":439},[422,1723,1725],{"class":424,"line":1724},18,[422,1726,923],{"class":439},[422,1728,1730],{"class":424,"line":1729},19,[422,1731,564],{"emptyLinePlaceholder":563},[422,1733,1735,1737,1739,1741,1743,1745,1747,1749,1751],{"class":424,"line":1734},20,[422,1736,984],{"class":446},[422,1738,573],{"class":439},[422,1740,577],{"class":576},[422,1742,580],{"class":439},[422,1744,1591],{"class":446},[422,1746,573],{"class":439},[422,1748,1596],{"class":446},[422,1750,590],{"class":439},[422,1752,1753],{"class":593},"    \u002F\u002F promoted field\n",[422,1755,1757,1759,1761,1763,1765,1767,1769,1772,1774],{"class":424,"line":1756},21,[422,1758,984],{"class":446},[422,1760,573],{"class":439},[422,1762,577],{"class":576},[422,1764,580],{"class":439},[422,1766,1591],{"class":446},[422,1768,573],{"class":439},[422,1770,1771],{"class":576},"Domain",[422,1773,1000],{"class":439},[422,1775,1776],{"class":593}," \u002F\u002F promoted method: example.com\n",[422,1778,1780],{"class":424,"line":1779},22,[422,1781,475],{"class":439},[395,1783,1784,1785,1788,1789,1792,1793,1795,1796,1800,1801,1803,1804,1806,1807,1809,1810,1812],{},"The embedded type's fields and methods are ",[399,1786,1787],{},"promoted",", so you can use them as if they belonged to ",[419,1790,1791],{},"Customer",". Embedding is composition: a ",[419,1794,1791],{}," ",[1797,1798,1799],"em",{},"has"," a ",[419,1802,1563],{},", and it is not a kind of ",[419,1805,1563],{},". If ",[419,1808,1791],{}," defines its own ",[419,1811,1771],{}," method, it takes priority.",[390,1814,1816],{"id":1815},"comparing-structs","Comparing structs",[395,1818,1819,1820,1823],{},"Two structs of the same type are equal with ",[419,1821,1822],{},"=="," when all their fields are equal. This only works if every field is comparable, so a struct containing a slice or map cannot be compared directly.",[390,1825,1827],{"id":1826},"exported-fields","Exported fields",[395,1829,1830,1831,1833,1834,1837],{},"The capitalisation rule applies to fields and methods as well. ",[419,1832,587],{}," can be read from other packages, while ",[419,1835,1836],{},"name"," cannot. Keep fields lowercase when you want callers to go through methods, so that you can enforce rules such as \"balance never goes negative\".",[390,1839,1841],{"id":1840},"common-mistakes","Common mistakes",[1843,1844,1845,1852,1858,1864,1873],"ul",{},[1846,1847,1848,1851],"li",{},[399,1849,1850],{},"Using a value receiver and expecting the struct to change."," Switch to a pointer receiver.",[1846,1853,1854,1857],{},[399,1855,1856],{},"Mixing value and pointer receivers"," on one type.",[1846,1859,1860,1863],{},[399,1861,1862],{},"Using positional struct literals"," that break when fields change.",[1846,1865,1866,1869,1870,573],{},[399,1867,1868],{},"Thinking embedding is inheritance."," There is no overriding through a type hierarchy and no ",[419,1871,1872],{},"super",[1846,1874,1875],{},[399,1876,1877],{},"Copying a struct that contains a mutex or other state that must stay unique.",[390,1879,1881],{"id":1880},"practice","Practice",[395,1883,1884,1885,1888,1889,1892,1893,1896,1897,1900,1901,1904,1905,1907],{},"Create a ",[419,1886,1887],{},"BankAccount"," struct with ",[419,1890,1891],{},"Owner"," and ",[419,1894,1895],{},"Balance"," fields. Add a ",[419,1898,1899],{},"Deposit"," method and a ",[419,1902,1903],{},"Withdraw"," method. ",[419,1906,1903],{}," should return an error if the amount is not positive or is larger than the balance. Show both a successful and a failing withdrawal.",[1909,1910,1911],"accordion",{},[1912,1913,1916,2476],"accordion-item",{"icon":1914,"label":1915},"i-lucide-lightbulb","Show solution",[412,1917,1919],{"className":414,"code":1918,"language":416,"meta":417,"style":417},"package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n)\n\ntype BankAccount struct {\n    Owner   string\n    Balance int\n}\n\nfunc (a *BankAccount) Deposit(amount int) {\n    a.Balance += amount\n}\n\nfunc (a *BankAccount) Withdraw(amount int) error {\n    if amount \u003C= 0 {\n        return errors.New(\"amount must be positive\")\n    }\n    if amount > a.Balance {\n        return errors.New(\"insufficient funds\")\n    }\n    a.Balance -= amount\n    return nil\n}\n\nfunc main() {\n    account := BankAccount{Owner: \"Mira\", Balance: 100}\n\n    account.Deposit(50)\n    fmt.Println(account.Balance) \u002F\u002F 150\n\n    if err := account.Withdraw(30); err != nil {\n        fmt.Println(\"error:\", err)\n    }\n    fmt.Println(account.Balance) \u002F\u002F 120\n\n    if err := account.Withdraw(500); err != nil {\n        fmt.Println(\"error:\", err) \u002F\u002F error: insufficient funds\n    }\n    fmt.Println(account.Balance) \u002F\u002F still 120\n}\n",[419,1920,1921,1929,1933,1941,1951,1959,1963,1967,1978,1985,1993,1997,2001,2030,2045,2049,2053,2083,2098,2121,2125,2143,2164,2169,2183,2192,2197,2202,2213,2247,2252,2268,2291,2296,2331,2356,2361,2383,2388,2418,2444,2449,2471],{"__ignoreMap":417},[422,1922,1923,1926],{"class":424,"line":425},[422,1924,1925],{"class":428},"package",[422,1927,1928],{"class":432}," main\n",[422,1930,1931],{"class":424,"line":443},[422,1932,564],{"emptyLinePlaceholder":563},[422,1934,1935,1938],{"class":424,"line":454},[422,1936,1937],{"class":890},"import",[422,1939,1940],{"class":439}," (\n",[422,1942,1943,1945,1949],{"class":424,"line":463},[422,1944,1715],{"class":531},[422,1946,1948],{"class":1947},"sdLNw","errors",[422,1950,787],{"class":531},[422,1952,1953,1955,1957],{"class":424,"line":472},[422,1954,1715],{"class":531},[422,1956,570],{"class":1947},[422,1958,787],{"class":531},[422,1960,1961],{"class":424,"line":560},[422,1962,1483],{"class":439},[422,1964,1965],{"class":424,"line":567},[422,1966,564],{"emptyLinePlaceholder":563},[422,1968,1969,1971,1974,1976],{"class":424,"line":597},[422,1970,429],{"class":428},[422,1972,1973],{"class":432}," BankAccount",[422,1975,436],{"class":428},[422,1977,440],{"class":439},[422,1979,1980,1983],{"class":424,"line":957},[422,1981,1982],{"class":446},"    Owner",[422,1984,460],{"class":450},[422,1986,1987,1990],{"class":424,"line":981},[422,1988,1989],{"class":446},"    Balance",[422,1991,1992],{"class":450}," int\n",[422,1994,1995],{"class":424,"line":1005},[422,1996,475],{"class":439},[422,1998,1999],{"class":424,"line":1010},[422,2000,564],{"emptyLinePlaceholder":563},[422,2002,2003,2005,2007,2010,2012,2014,2016,2019,2021,2024,2026,2028],{"class":424,"line":1021},[422,2004,853],{"class":428},[422,2006,866],{"class":439},[422,2008,2009],{"class":869},"a ",[422,2011,1141],{"class":428},[422,2013,1887],{"class":432},[422,2015,590],{"class":439},[422,2017,2018],{"class":576}," Deposit",[422,2020,580],{"class":439},[422,2022,2023],{"class":869},"amount",[422,2025,1261],{"class":450},[422,2027,590],{"class":439},[422,2029,440],{"class":439},[422,2031,2032,2035,2037,2039,2042],{"class":424,"line":1044},[422,2033,2034],{"class":446},"    a",[422,2036,573],{"class":439},[422,2038,1895],{"class":446},[422,2040,2041],{"class":502}," +=",[422,2043,2044],{"class":446}," amount\n",[422,2046,2047],{"class":424,"line":1669},[422,2048,475],{"class":439},[422,2050,2051],{"class":424,"line":1681},[422,2052,564],{"emptyLinePlaceholder":563},[422,2054,2055,2057,2059,2061,2063,2065,2067,2070,2072,2074,2076,2078,2081],{"class":424,"line":1707},[422,2056,853],{"class":428},[422,2058,866],{"class":439},[422,2060,2009],{"class":869},[422,2062,1141],{"class":428},[422,2064,1887],{"class":432},[422,2066,590],{"class":439},[422,2068,2069],{"class":576}," Withdraw",[422,2071,580],{"class":439},[422,2073,2023],{"class":869},[422,2075,1261],{"class":450},[422,2077,590],{"class":439},[422,2079,2080],{"class":450}," error",[422,2082,440],{"class":439},[422,2084,2085,2087,2090,2093,2096],{"class":424,"line":1724},[422,2086,891],{"class":890},[422,2088,2089],{"class":446}," amount",[422,2091,2092],{"class":900}," \u003C=",[422,2094,2095],{"class":518}," 0",[422,2097,440],{"class":439},[422,2099,2100,2102,2105,2107,2110,2112,2114,2117,2119],{"class":424,"line":1729},[422,2101,911],{"class":890},[422,2103,2104],{"class":446}," errors",[422,2106,573],{"class":439},[422,2108,2109],{"class":576},"New",[422,2111,580],{"class":439},[422,2113,539],{"class":531},[422,2115,2116],{"class":535},"amount must be positive",[422,2118,539],{"class":531},[422,2120,1483],{"class":439},[422,2122,2123],{"class":424,"line":1734},[422,2124,923],{"class":439},[422,2126,2127,2129,2131,2134,2137,2139,2141],{"class":424,"line":1756},[422,2128,891],{"class":890},[422,2130,2089],{"class":446},[422,2132,2133],{"class":900}," >",[422,2135,2136],{"class":446}," a",[422,2138,573],{"class":439},[422,2140,1895],{"class":446},[422,2142,440],{"class":439},[422,2144,2145,2147,2149,2151,2153,2155,2157,2160,2162],{"class":424,"line":1779},[422,2146,911],{"class":890},[422,2148,2104],{"class":446},[422,2150,573],{"class":439},[422,2152,2109],{"class":576},[422,2154,580],{"class":439},[422,2156,539],{"class":531},[422,2158,2159],{"class":535},"insufficient funds",[422,2161,539],{"class":531},[422,2163,1483],{"class":439},[422,2165,2167],{"class":424,"line":2166},23,[422,2168,923],{"class":439},[422,2170,2172,2174,2176,2178,2181],{"class":424,"line":2171},24,[422,2173,2034],{"class":446},[422,2175,573],{"class":439},[422,2177,1895],{"class":446},[422,2179,2180],{"class":502}," -=",[422,2182,2044],{"class":446},[422,2184,2186,2188],{"class":424,"line":2185},25,[422,2187,928],{"class":890},[422,2189,2191],{"class":2190},"sp8Z5"," nil\n",[422,2193,2195],{"class":424,"line":2194},26,[422,2196,475],{"class":439},[422,2198,2200],{"class":424,"line":2199},27,[422,2201,564],{"emptyLinePlaceholder":563},[422,2203,2205,2207,2209,2211],{"class":424,"line":2204},28,[422,2206,853],{"class":428},[422,2208,950],{"class":576},[422,2210,880],{"class":439},[422,2212,440],{"class":439},[422,2214,2216,2219,2221,2223,2225,2227,2229,2231,2233,2235,2237,2240,2242,2245],{"class":424,"line":2215},29,[422,2217,2218],{"class":446},"    account",[422,2220,503],{"class":502},[422,2222,1973],{"class":432},[422,2224,681],{"class":439},[422,2226,1891],{"class":446},[422,2228,515],{"class":439},[422,2230,609],{"class":531},[422,2232,536],{"class":535},[422,2234,539],{"class":531},[422,2236,810],{"class":439},[422,2238,2239],{"class":446}," Balance",[422,2241,515],{"class":439},[422,2243,2244],{"class":518}," 100",[422,2246,475],{"class":439},[422,2248,2250],{"class":424,"line":2249},30,[422,2251,564],{"emptyLinePlaceholder":563},[422,2253,2255,2257,2259,2261,2263,2266],{"class":424,"line":2254},31,[422,2256,2218],{"class":446},[422,2258,573],{"class":439},[422,2260,1899],{"class":576},[422,2262,580],{"class":439},[422,2264,2265],{"class":518},"50",[422,2267,1483],{"class":439},[422,2269,2271,2273,2275,2277,2279,2282,2284,2286,2288],{"class":424,"line":2270},32,[422,2272,984],{"class":446},[422,2274,573],{"class":439},[422,2276,577],{"class":576},[422,2278,580],{"class":439},[422,2280,2281],{"class":446},"account",[422,2283,573],{"class":439},[422,2285,1895],{"class":446},[422,2287,590],{"class":439},[422,2289,2290],{"class":593}," \u002F\u002F 150\n",[422,2292,2294],{"class":424,"line":2293},33,[422,2295,564],{"emptyLinePlaceholder":563},[422,2297,2299,2301,2304,2306,2309,2311,2313,2315,2318,2321,2323,2326,2329],{"class":424,"line":2298},34,[422,2300,891],{"class":890},[422,2302,2303],{"class":446}," err",[422,2305,503],{"class":502},[422,2307,2308],{"class":446}," account",[422,2310,573],{"class":439},[422,2312,1903],{"class":576},[422,2314,580],{"class":439},[422,2316,2317],{"class":518},"30",[422,2319,2320],{"class":439},");",[422,2322,2303],{"class":446},[422,2324,2325],{"class":900}," !=",[422,2327,2328],{"class":2190}," nil",[422,2330,440],{"class":439},[422,2332,2334,2337,2339,2341,2343,2345,2348,2350,2352,2354],{"class":424,"line":2333},35,[422,2335,2336],{"class":446},"        fmt",[422,2338,573],{"class":439},[422,2340,577],{"class":576},[422,2342,580],{"class":439},[422,2344,539],{"class":531},[422,2346,2347],{"class":535},"error:",[422,2349,539],{"class":531},[422,2351,810],{"class":439},[422,2353,2303],{"class":446},[422,2355,1483],{"class":439},[422,2357,2359],{"class":424,"line":2358},36,[422,2360,923],{"class":439},[422,2362,2364,2366,2368,2370,2372,2374,2376,2378,2380],{"class":424,"line":2363},37,[422,2365,984],{"class":446},[422,2367,573],{"class":439},[422,2369,577],{"class":576},[422,2371,580],{"class":439},[422,2373,2281],{"class":446},[422,2375,573],{"class":439},[422,2377,1895],{"class":446},[422,2379,590],{"class":439},[422,2381,2382],{"class":593}," \u002F\u002F 120\n",[422,2384,2386],{"class":424,"line":2385},38,[422,2387,564],{"emptyLinePlaceholder":563},[422,2389,2391,2393,2395,2397,2399,2401,2403,2405,2408,2410,2412,2414,2416],{"class":424,"line":2390},39,[422,2392,891],{"class":890},[422,2394,2303],{"class":446},[422,2396,503],{"class":502},[422,2398,2308],{"class":446},[422,2400,573],{"class":439},[422,2402,1903],{"class":576},[422,2404,580],{"class":439},[422,2406,2407],{"class":518},"500",[422,2409,2320],{"class":439},[422,2411,2303],{"class":446},[422,2413,2325],{"class":900},[422,2415,2328],{"class":2190},[422,2417,440],{"class":439},[422,2419,2421,2423,2425,2427,2429,2431,2433,2435,2437,2439,2441],{"class":424,"line":2420},40,[422,2422,2336],{"class":446},[422,2424,573],{"class":439},[422,2426,577],{"class":576},[422,2428,580],{"class":439},[422,2430,539],{"class":531},[422,2432,2347],{"class":535},[422,2434,539],{"class":531},[422,2436,810],{"class":439},[422,2438,2303],{"class":446},[422,2440,590],{"class":439},[422,2442,2443],{"class":593}," \u002F\u002F error: insufficient funds\n",[422,2445,2447],{"class":424,"line":2446},41,[422,2448,923],{"class":439},[422,2450,2452,2454,2456,2458,2460,2462,2464,2466,2468],{"class":424,"line":2451},42,[422,2453,984],{"class":446},[422,2455,573],{"class":439},[422,2457,577],{"class":576},[422,2459,580],{"class":439},[422,2461,2281],{"class":446},[422,2463,573],{"class":439},[422,2465,1895],{"class":446},[422,2467,590],{"class":439},[422,2469,2470],{"class":593}," \u002F\u002F still 120\n",[422,2472,2474],{"class":424,"line":2473},43,[422,2475,475],{"class":439},[395,2477,2478,2479,2481,2482,2485,2486,2489,2490,2493],{},"Both methods use pointer receivers because they change the balance. If ",[419,2480,1899],{}," used a value receiver, the deposit would be lost when the method returned. The ",[419,2483,2484],{},"error"," return type and ",[419,2487,2488],{},"errors.New"," are covered in the errors lesson, but the pattern is the one from the functions lesson: return ",[419,2491,2492],{},"nil"," on success and an error on failure.",[390,2495,2497],{"id":2496},"summary","Summary",[395,2499,2500],{},"A struct groups named fields into a new type, and Go sets any missing field to its zero value. Structs are copied on assignment, so use a pointer when you want to share one. A method is a function with a receiver, and the receiver should be a pointer if the method changes the struct or the struct is large. Go has no constructors, only ordinary functions by convention, and no inheritance, only embedding, which promotes the inner type's fields and methods.",[395,2502,2503,2504,1892,2510,573],{},"Official reference: ",[740,2505,2509],{"href":2506,"rel":2507},"https:\u002F\u002Fgo.dev\u002Ftour\u002Fmethods\u002F1",[2508],"nofollow","A Tour of Go: Methods",[740,2511,2514],{"href":2512,"rel":2513},"https:\u002F\u002Fgo.dev\u002Fdoc\u002Feffective_go#embedding",[2508],"Effective Go: Embedding",[2516,2517],"learning-lesson-complete",{},[2519,2520],"learning-lesson-navigation",{"next":56,"nextTitle":55,"previous":111,"previousTitle":110},[2522,2523,2524],"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 .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 .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 .sqQ1y, html code.shiki .sqQ1y{--shiki-light:#B58900;--shiki-default:#FF9CAC;--shiki-dark:#D19A66}html pre.shiki code .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}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 .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 .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 .st3jj, html code.shiki .st3jj{--shiki-light:#CB4B16;--shiki-default:#F07178;--shiki-dark:#D19A66}html pre.shiki code .sk51v, html code.shiki .sk51v{--shiki-light:#CB4B16;--shiki-default:#EEFFFF;--shiki-dark:#56B6C2}html pre.shiki code .sdLNw, html code.shiki .sdLNw{--shiki-light:#2AA198;--shiki-default:#FFCB6B;--shiki-dark:#98C379}html pre.shiki code .sp8Z5, html code.shiki .sp8Z5{--shiki-light:#B58900;--shiki-default:#89DDFF;--shiki-dark:#D19A66}",{"title":417,"searchDepth":443,"depth":443,"links":2526},[2527,2528,2529,2530,2531,2532,2533,2534,2535,2536,2537,2538,2539,2540],{"id":392,"depth":443,"text":393},{"id":409,"depth":443,"text":410},{"id":627,"depth":443,"text":628},{"id":722,"depth":443,"text":723},{"id":839,"depth":443,"text":840},{"id":1065,"depth":443,"text":1066},{"id":1234,"depth":443,"text":1235},{"id":1323,"depth":443,"text":1324},{"id":1511,"depth":443,"text":1512},{"id":1815,"depth":443,"text":1816},{"id":1826,"depth":443,"text":1827},{"id":1840,"depth":443,"text":1841},{"id":1880,"depth":443,"text":1881},{"id":2496,"depth":443,"text":2497},"Model related data with structs, attach behaviour with methods, and compose types with embedding.","md",null,{},{"icon":53},{"description":2547,"title":50},"Learn Go structs, keyed literals, zero values, methods, value versus pointer receivers, constructor functions, the Stringer interface, embedding, and struct comparison.","VGJkBvv7btajDeGk7dJismNDoGyHG_SJnFerW0hyXl0",[2550,2552],{"title":45,"path":46,"stem":47,"description":2551,"icon":48,"children":-1},"Take a Go program apart to see how packages, imports, declarations, and the main entry point fit together.",{"title":55,"path":56,"stem":57,"description":2553,"icon":58,"children":-1},"Describe behaviour with small interfaces, understand implicit implementation, and avoid the typed-nil trap.",1794118096900]