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