[{"data":1,"prerenderedAt":2200},["ShallowReactive",2],{"navigation_docs":3,"-fundamentals-defined-types":377,"-fundamentals-defined-types-surround":2195},[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":65,"body":379,"description":2187,"extension":2188,"links":2189,"meta":2190,"navigation":2191,"path":66,"seo":2192,"stem":67,"__hash__":2194},"docs\u002F2.fundamentals\u002F13.defined-types.md",{"type":380,"value":381,"toc":2172},"minimark",[382,389,394,407,411,414,476,483,514,517,521,548,562,717,725,728,732,735,810,813,817,824,845,848,852,855,994,1000,1006,1013,1085,1103,1110,1340,1352,1356,1359,1394,1397,1521,1524,1528,1539,1556,1566,1596,1599,1603,1637,1641,1665,2133,2137,2147,2162,2165,2168],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Use defined types to give plain values a meaning the compiler can check.","Intermediate","Variables and Data Types, Functions, Structs and Methods","40 minutes",[390,391,393],"h2",{"id":392},"what-you-will-learn","What you will learn",[395,396,397,398,402,403,406],"p",{},"A number can mean many things: a user ID, a price, a temperature. If they all share the type ",[399,400,401],"code",{},"int",", the compiler cannot warn you when you mix them up. This lesson shows how to create new types on top of existing ones so the compiler can tell them apart, how to attach methods to them, how to build enum-like sets of constants with ",[399,404,405],{},"iota",", and how a type alias differs from a defined type.",[390,408,410],{"id":409},"the-problem-same-representation-different-meaning","The problem: same representation, different meaning",[395,412,413],{},"Consider this function:",[415,416,421],"pre",{"className":417,"code":418,"language":419,"meta":420,"style":420},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","func transfer(fromAccount int64, toAccount int64, amount int64) { ... }\n","go","",[399,422,423],{"__ignoreMap":420},[424,425,428,432,436,440,444,448,451,454,456,458,461,463,466,469,473],"span",{"class":426,"line":427},"line",1,[424,429,431],{"class":430},"sEOdo","func",[424,433,435],{"class":434},"sisdC"," transfer",[424,437,439],{"class":438},"shFME","(",[424,441,443],{"class":442},"syre3","fromAccount",[424,445,447],{"class":446},"sF6sA"," int64",[424,449,450],{"class":438},",",[424,452,453],{"class":442}," toAccount",[424,455,447],{"class":446},[424,457,450],{"class":438},[424,459,460],{"class":442}," amount",[424,462,447],{"class":446},[424,464,465],{"class":438},")",[424,467,468],{"class":438}," {",[424,470,472],{"class":471},"sN42N"," ...",[424,474,475],{"class":438}," }\n",[395,477,478,479,482],{},"Every parameter is an ",[399,480,481],{},"int64",", so this call compiles even though the arguments are in the wrong order:",[415,484,486],{"className":417,"code":485,"language":419,"meta":420,"style":420},"transfer(amount, fromAccount, toAccount) \u002F\u002F compiles, wrong, and expensive\n",[399,487,488],{"__ignoreMap":420},[424,489,490,493,495,499,501,504,506,508,510],{"class":426,"line":427},[424,491,492],{"class":434},"transfer",[424,494,439],{"class":438},[424,496,498],{"class":497},"s45-6","amount",[424,500,450],{"class":438},[424,502,503],{"class":497}," fromAccount",[424,505,450],{"class":438},[424,507,453],{"class":497},[424,509,465],{"class":438},[424,511,513],{"class":512},"sJsHy"," \u002F\u002F compiles, wrong, and expensive\n",[395,515,516],{},"Go lets you give each concept its own type, so a mix-up becomes a compile error instead of a production bug.",[390,518,520],{"id":519},"defining-a-type","Defining a type",[415,522,524],{"className":417,"code":523,"language":419,"meta":420,"style":420},"type UserID int64\ntype ProductID int64\n",[399,525,526,538],{"__ignoreMap":420},[424,527,528,531,535],{"class":426,"line":427},[424,529,530],{"class":430},"type",[424,532,534],{"class":533},"snwKR"," UserID",[424,536,537],{"class":446}," int64\n",[424,539,541,543,546],{"class":426,"line":540},2,[424,542,530],{"class":430},[424,544,545],{"class":533}," ProductID",[424,547,537],{"class":446},[395,549,550,553,554,558,559,561],{},[399,551,552],{},"UserID"," is a new type whose ",[555,556,557],"strong",{},"underlying type"," is ",[399,560,481],{},". It has the same memory layout and supports the same operators, but the compiler treats it as a distinct type:",[415,563,565],{"className":417,"code":564,"language":419,"meta":420,"style":420},"func loadUser(id UserID) {\n    fmt.Println(\"loading user\", id)\n}\n\nfunc main() {\n    var userID UserID = 42\n    var productID ProductID = 42\n\n    loadUser(userID)    \u002F\u002F fine\n    loadUser(productID) \u002F\u002F compile error\n}\n",[399,566,567,586,617,623,630,643,662,676,681,697,712],{"__ignoreMap":420},[424,568,569,571,574,576,579,581,583],{"class":426,"line":427},[424,570,431],{"class":430},[424,572,573],{"class":434}," loadUser",[424,575,439],{"class":438},[424,577,578],{"class":442},"id",[424,580,534],{"class":533},[424,582,465],{"class":438},[424,584,585],{"class":438}," {\n",[424,587,588,591,594,597,599,603,607,609,611,614],{"class":426,"line":540},[424,589,590],{"class":497},"    fmt",[424,592,593],{"class":438},".",[424,595,596],{"class":434},"Println",[424,598,439],{"class":438},[424,600,602],{"class":601},"sED5a","\"",[424,604,606],{"class":605},"sVazx","loading user",[424,608,602],{"class":601},[424,610,450],{"class":438},[424,612,613],{"class":497}," id",[424,615,616],{"class":438},")\n",[424,618,620],{"class":426,"line":619},3,[424,621,622],{"class":438},"}\n",[424,624,626],{"class":426,"line":625},4,[424,627,629],{"emptyLinePlaceholder":628},true,"\n",[424,631,633,635,638,641],{"class":426,"line":632},5,[424,634,431],{"class":430},[424,636,637],{"class":434}," main",[424,639,640],{"class":438},"()",[424,642,585],{"class":438},[424,644,646,649,652,654,658],{"class":426,"line":645},6,[424,647,648],{"class":430},"    var",[424,650,651],{"class":497}," userID",[424,653,534],{"class":533},[424,655,657],{"class":656},"su2KV"," =",[424,659,661],{"class":660},"sAJzr"," 42\n",[424,663,665,667,670,672,674],{"class":426,"line":664},7,[424,666,648],{"class":430},[424,668,669],{"class":497}," productID",[424,671,545],{"class":533},[424,673,657],{"class":656},[424,675,661],{"class":660},[424,677,679],{"class":426,"line":678},8,[424,680,629],{"emptyLinePlaceholder":628},[424,682,684,687,689,692,694],{"class":426,"line":683},9,[424,685,686],{"class":434},"    loadUser",[424,688,439],{"class":438},[424,690,691],{"class":497},"userID",[424,693,465],{"class":438},[424,695,696],{"class":512},"    \u002F\u002F fine\n",[424,698,700,702,704,707,709],{"class":426,"line":699},10,[424,701,686],{"class":434},[424,703,439],{"class":438},[424,705,706],{"class":497},"productID",[424,708,465],{"class":438},[424,710,711],{"class":512}," \u002F\u002F compile error\n",[424,713,715],{"class":426,"line":714},11,[424,716,622],{"class":438},[415,718,723],{"className":719,"code":721,"language":722,"meta":420},[720],"language-text","cannot use productID (variable of type ProductID) as UserID value in argument to loadUser\n","text",[399,724,721],{"__ignoreMap":420},[395,726,727],{},"The exact wording of the message varies a little between Go versions, but the point is the same: the two types are not interchangeable even though both are numbers underneath.",[390,729,731],{"id":730},"conversions-are-explicit","Conversions are explicit",[395,733,734],{},"Moving between a defined type and its underlying type requires a conversion:",[415,736,738],{"className":417,"code":737,"language":419,"meta":420,"style":420},"var raw int64 = 42\nvar id UserID = raw \u002F\u002F compile error: different types\n\nid = UserID(raw)    \u002F\u002F fine, explicit\nback := int64(id)   \u002F\u002F fine, explicit\n",[399,739,740,754,769,773,791],{"__ignoreMap":420},[424,741,742,745,748,750,752],{"class":426,"line":427},[424,743,744],{"class":430},"var",[424,746,747],{"class":497}," raw",[424,749,447],{"class":446},[424,751,657],{"class":656},[424,753,661],{"class":660},[424,755,756,758,760,762,764,766],{"class":426,"line":540},[424,757,744],{"class":430},[424,759,613],{"class":497},[424,761,534],{"class":533},[424,763,657],{"class":656},[424,765,747],{"class":497},[424,767,768],{"class":512}," \u002F\u002F compile error: different types\n",[424,770,771],{"class":426,"line":619},[424,772,629],{"emptyLinePlaceholder":628},[424,774,775,777,779,781,783,786,788],{"class":426,"line":625},[424,776,578],{"class":497},[424,778,657],{"class":656},[424,780,534],{"class":434},[424,782,439],{"class":438},[424,784,785],{"class":497},"raw",[424,787,465],{"class":438},[424,789,790],{"class":512},"    \u002F\u002F fine, explicit\n",[424,792,793,796,799,801,803,805,807],{"class":426,"line":632},[424,794,795],{"class":497},"back",[424,797,798],{"class":656}," :=",[424,800,447],{"class":446},[424,802,439],{"class":438},[424,804,578],{"class":497},[424,806,465],{"class":438},[424,808,809],{"class":512},"   \u002F\u002F fine, explicit\n",[395,811,812],{},"A conversion does not change the number, only the type the compiler sees. Writing it deliberately forces you to pause at the places where meaning crosses a boundary, such as reading an ID from a database or a URL.",[390,814,816],{"id":815},"constants-adapt","Constants adapt",[395,818,819,820,823],{},"An untyped constant, like a literal ",[399,821,822],{},"42",", can be used wherever its value fits:",[415,825,827],{"className":417,"code":826,"language":419,"meta":420,"style":420},"var id UserID = 42 \u002F\u002F fine: the constant 42 becomes a UserID\n",[399,828,829],{"__ignoreMap":420},[424,830,831,833,835,837,839,842],{"class":426,"line":427},[424,832,744],{"class":430},[424,834,613],{"class":497},[424,836,534],{"class":533},[424,838,657],{"class":656},[424,840,841],{"class":660}," 42",[424,843,844],{"class":512}," \u002F\u002F fine: the constant 42 becomes a UserID\n",[395,846,847],{},"Once a value sits in a typed variable, though, the strict rules apply.",[390,849,851],{"id":850},"adding-methods","Adding methods",[395,853,854],{},"You can attach methods to a defined type declared in your own package:",[415,856,858],{"className":417,"code":857,"language":419,"meta":420,"style":420},"type Celsius float64\n\nfunc (c Celsius) Fahrenheit() float64 {\n    return float64(c)*9\u002F5 + 32\n}\n\nfunc main() {\n    boiling := Celsius(100)\n    fmt.Println(boiling.Fahrenheit()) \u002F\u002F 212\n}\n",[399,859,860,870,874,899,932,936,940,950,966,990],{"__ignoreMap":420},[424,861,862,864,867],{"class":426,"line":427},[424,863,530],{"class":430},[424,865,866],{"class":533}," Celsius",[424,868,869],{"class":446}," float64\n",[424,871,872],{"class":426,"line":540},[424,873,629],{"emptyLinePlaceholder":628},[424,875,876,878,881,884,887,889,892,894,897],{"class":426,"line":619},[424,877,431],{"class":430},[424,879,880],{"class":438}," (",[424,882,883],{"class":442},"c ",[424,885,886],{"class":533},"Celsius",[424,888,465],{"class":438},[424,890,891],{"class":434}," Fahrenheit",[424,893,640],{"class":438},[424,895,896],{"class":446}," float64",[424,898,585],{"class":438},[424,900,901,905,907,909,912,914,917,920,923,926,929],{"class":426,"line":625},[424,902,904],{"class":903},"s7Bcg","    return",[424,906,896],{"class":446},[424,908,439],{"class":438},[424,910,911],{"class":497},"c",[424,913,465],{"class":438},[424,915,916],{"class":430},"*",[424,918,919],{"class":660},"9",[424,921,922],{"class":430},"\u002F",[424,924,925],{"class":660},"5",[424,927,928],{"class":430}," +",[424,930,931],{"class":660}," 32\n",[424,933,934],{"class":426,"line":632},[424,935,622],{"class":438},[424,937,938],{"class":426,"line":645},[424,939,629],{"emptyLinePlaceholder":628},[424,941,942,944,946,948],{"class":426,"line":664},[424,943,431],{"class":430},[424,945,637],{"class":434},[424,947,640],{"class":438},[424,949,585],{"class":438},[424,951,952,955,957,959,961,964],{"class":426,"line":678},[424,953,954],{"class":497},"    boiling",[424,956,798],{"class":656},[424,958,866],{"class":434},[424,960,439],{"class":438},[424,962,963],{"class":660},"100",[424,965,616],{"class":438},[424,967,968,970,972,974,976,979,981,984,987],{"class":426,"line":683},[424,969,590],{"class":497},[424,971,593],{"class":438},[424,973,596],{"class":434},[424,975,439],{"class":438},[424,977,978],{"class":497},"boiling",[424,980,593],{"class":438},[424,982,983],{"class":434},"Fahrenheit",[424,985,986],{"class":438},"())",[424,988,989],{"class":512}," \u002F\u002F 212\n",[424,991,992],{"class":426,"line":699},[424,993,622],{"class":438},[395,995,996,997,999],{},"This keeps behaviour next to the concept it belongs to. You cannot add methods to built-in types like ",[399,998,401],{}," directly, which is one more reason to define a type.",[390,1001,1003,1004],{"id":1002},"enum-like-constants-with-iota","Enum-like constants with ",[399,1005,405],{},[395,1007,1008,1009,1012],{},"Go has no ",[399,1010,1011],{},"enum"," keyword. The idiom is a defined type plus a block of constants:",[415,1014,1016],{"className":417,"code":1015,"language":419,"meta":420,"style":420},"type Weekday int\n\nconst (\n    Sunday Weekday = iota \u002F\u002F 0\n    Monday                \u002F\u002F 1\n    Tuesday               \u002F\u002F 2\n    Wednesday             \u002F\u002F 3\n)\n",[399,1017,1018,1028,1032,1040,1057,1065,1073,1081],{"__ignoreMap":420},[424,1019,1020,1022,1025],{"class":426,"line":427},[424,1021,530],{"class":430},[424,1023,1024],{"class":533}," Weekday",[424,1026,1027],{"class":446}," int\n",[424,1029,1030],{"class":426,"line":540},[424,1031,629],{"emptyLinePlaceholder":628},[424,1033,1034,1037],{"class":426,"line":619},[424,1035,1036],{"class":430},"const",[424,1038,1039],{"class":438}," (\n",[424,1041,1042,1046,1048,1050,1054],{"class":426,"line":625},[424,1043,1045],{"class":1044},"sry10","    Sunday",[424,1047,1024],{"class":533},[424,1049,657],{"class":656},[424,1051,1053],{"class":1052},"sp8Z5"," iota",[424,1055,1056],{"class":512}," \u002F\u002F 0\n",[424,1058,1059,1062],{"class":426,"line":632},[424,1060,1061],{"class":1044},"    Monday",[424,1063,1064],{"class":512},"                \u002F\u002F 1\n",[424,1066,1067,1070],{"class":426,"line":645},[424,1068,1069],{"class":1044},"    Tuesday",[424,1071,1072],{"class":512},"               \u002F\u002F 2\n",[424,1074,1075,1078],{"class":426,"line":664},[424,1076,1077],{"class":1044},"    Wednesday",[424,1079,1080],{"class":512},"             \u002F\u002F 3\n",[424,1082,1083],{"class":426,"line":678},[424,1084,616],{"class":438},[395,1086,1087,1089,1090,1092,1093,558,1096,1099,1100,1102],{},[399,1088,405],{}," is a counter that starts at 0 in each ",[399,1091,1036],{}," block and increases by one for every line. Lines that omit the expression repeat the previous one, so ",[399,1094,1095],{},"Monday",[399,1097,1098],{},"Weekday = iota"," with ",[399,1101,405],{}," now equal to 1.",[395,1104,1105,1106,1109],{},"Add a ",[399,1107,1108],{},"String"," method and the values print nicely:",[415,1111,1113],{"className":417,"code":1112,"language":419,"meta":420,"style":420},"func (d Weekday) String() string {\n    switch d {\n    case Sunday:\n        return \"Sunday\"\n    case Monday:\n        return \"Monday\"\n    case Tuesday:\n        return \"Tuesday\"\n    case Wednesday:\n        return \"Wednesday\"\n    }\n    return \"Unknown\"\n}\n\nfunc main() {\n    day := Tuesday\n    fmt.Println(day)        \u002F\u002F Tuesday\n    fmt.Printf(\"%d\\n\", day) \u002F\u002F 2\n}\n",[399,1114,1115,1139,1149,1160,1174,1183,1193,1202,1213,1222,1233,1238,1250,1255,1260,1271,1282,1301,1335],{"__ignoreMap":420},[424,1116,1117,1119,1121,1124,1127,1129,1132,1134,1137],{"class":426,"line":427},[424,1118,431],{"class":430},[424,1120,880],{"class":438},[424,1122,1123],{"class":442},"d ",[424,1125,1126],{"class":533},"Weekday",[424,1128,465],{"class":438},[424,1130,1131],{"class":434}," String",[424,1133,640],{"class":438},[424,1135,1136],{"class":446}," string",[424,1138,585],{"class":438},[424,1140,1141,1144,1147],{"class":426,"line":540},[424,1142,1143],{"class":903},"    switch",[424,1145,1146],{"class":497}," d",[424,1148,585],{"class":438},[424,1150,1151,1154,1157],{"class":426,"line":619},[424,1152,1153],{"class":903},"    case",[424,1155,1156],{"class":497}," Sunday",[424,1158,1159],{"class":438},":\n",[424,1161,1162,1165,1168,1171],{"class":426,"line":625},[424,1163,1164],{"class":903},"        return",[424,1166,1167],{"class":601}," \"",[424,1169,1170],{"class":605},"Sunday",[424,1172,1173],{"class":601},"\"\n",[424,1175,1176,1178,1181],{"class":426,"line":632},[424,1177,1153],{"class":903},[424,1179,1180],{"class":497}," Monday",[424,1182,1159],{"class":438},[424,1184,1185,1187,1189,1191],{"class":426,"line":645},[424,1186,1164],{"class":903},[424,1188,1167],{"class":601},[424,1190,1095],{"class":605},[424,1192,1173],{"class":601},[424,1194,1195,1197,1200],{"class":426,"line":664},[424,1196,1153],{"class":903},[424,1198,1199],{"class":497}," Tuesday",[424,1201,1159],{"class":438},[424,1203,1204,1206,1208,1211],{"class":426,"line":678},[424,1205,1164],{"class":903},[424,1207,1167],{"class":601},[424,1209,1210],{"class":605},"Tuesday",[424,1212,1173],{"class":601},[424,1214,1215,1217,1220],{"class":426,"line":683},[424,1216,1153],{"class":903},[424,1218,1219],{"class":497}," Wednesday",[424,1221,1159],{"class":438},[424,1223,1224,1226,1228,1231],{"class":426,"line":699},[424,1225,1164],{"class":903},[424,1227,1167],{"class":601},[424,1229,1230],{"class":605},"Wednesday",[424,1232,1173],{"class":601},[424,1234,1235],{"class":426,"line":714},[424,1236,1237],{"class":438},"    }\n",[424,1239,1241,1243,1245,1248],{"class":426,"line":1240},12,[424,1242,904],{"class":903},[424,1244,1167],{"class":601},[424,1246,1247],{"class":605},"Unknown",[424,1249,1173],{"class":601},[424,1251,1253],{"class":426,"line":1252},13,[424,1254,622],{"class":438},[424,1256,1258],{"class":426,"line":1257},14,[424,1259,629],{"emptyLinePlaceholder":628},[424,1261,1263,1265,1267,1269],{"class":426,"line":1262},15,[424,1264,431],{"class":430},[424,1266,637],{"class":434},[424,1268,640],{"class":438},[424,1270,585],{"class":438},[424,1272,1274,1277,1279],{"class":426,"line":1273},16,[424,1275,1276],{"class":497},"    day",[424,1278,798],{"class":656},[424,1280,1281],{"class":497}," Tuesday\n",[424,1283,1285,1287,1289,1291,1293,1296,1298],{"class":426,"line":1284},17,[424,1286,590],{"class":497},[424,1288,593],{"class":438},[424,1290,596],{"class":434},[424,1292,439],{"class":438},[424,1294,1295],{"class":497},"day",[424,1297,465],{"class":438},[424,1299,1300],{"class":512},"        \u002F\u002F Tuesday\n",[424,1302,1304,1306,1308,1311,1313,1315,1319,1323,1325,1327,1330,1332],{"class":426,"line":1303},18,[424,1305,590],{"class":497},[424,1307,593],{"class":438},[424,1309,1310],{"class":434},"Printf",[424,1312,439],{"class":438},[424,1314,602],{"class":601},[424,1316,1318],{"class":1317},"st3jj","%d",[424,1320,1322],{"class":1321},"sk51v","\\n",[424,1324,602],{"class":601},[424,1326,450],{"class":438},[424,1328,1329],{"class":497}," day",[424,1331,465],{"class":438},[424,1333,1334],{"class":512}," \u002F\u002F 2\n",[424,1336,1338],{"class":426,"line":1337},19,[424,1339,622],{"class":438},[395,1341,1342,1343,1345,1346,1348,1349,1351],{},"Because ",[399,1344,1126],{}," is its own type, a function that takes a ",[399,1347,1126],{}," will not accept an arbitrary ",[399,1350,401],{}," variable, which is exactly the protection you want from an enum.",[390,1353,1355],{"id":1354},"types-do-not-validate-by-themselves","Types do not validate by themselves",[395,1357,1358],{},"A defined type does not restrict its values. This compiles:",[415,1360,1362],{"className":417,"code":1361,"language":419,"meta":420,"style":420},"type Percentage int\n\nvar p Percentage = 500 \u002F\u002F valid to the compiler\n",[399,1363,1364,1373,1377],{"__ignoreMap":420},[424,1365,1366,1368,1371],{"class":426,"line":427},[424,1367,530],{"class":430},[424,1369,1370],{"class":533}," Percentage",[424,1372,1027],{"class":446},[424,1374,1375],{"class":426,"line":540},[424,1376,629],{"emptyLinePlaceholder":628},[424,1378,1379,1381,1384,1386,1388,1391],{"class":426,"line":619},[424,1380,744],{"class":430},[424,1382,1383],{"class":497}," p",[424,1385,1370],{"class":533},[424,1387,657],{"class":656},[424,1389,1390],{"class":660}," 500",[424,1392,1393],{"class":512}," \u002F\u002F valid to the compiler\n",[395,1395,1396],{},"When values must follow a rule, enforce it in a constructor or parsing function:",[415,1398,1400],{"className":417,"code":1399,"language":419,"meta":420,"style":420},"func NewPercentage(value int) (Percentage, error) {\n    if value \u003C 0 || value > 100 {\n        return 0, fmt.Errorf(\"percentage %d is outside 0..100\", value)\n    }\n    return Percentage(value), nil\n}\n",[399,1401,1402,1433,1461,1497,1501,1517],{"__ignoreMap":420},[424,1403,1404,1406,1409,1411,1414,1417,1419,1421,1424,1426,1429,1431],{"class":426,"line":427},[424,1405,431],{"class":430},[424,1407,1408],{"class":434}," NewPercentage",[424,1410,439],{"class":438},[424,1412,1413],{"class":442},"value",[424,1415,1416],{"class":446}," int",[424,1418,465],{"class":438},[424,1420,880],{"class":438},[424,1422,1423],{"class":533},"Percentage",[424,1425,450],{"class":438},[424,1427,1428],{"class":446}," error",[424,1430,465],{"class":438},[424,1432,585],{"class":438},[424,1434,1435,1438,1441,1445,1448,1451,1453,1456,1459],{"class":426,"line":540},[424,1436,1437],{"class":903},"    if",[424,1439,1440],{"class":497}," value",[424,1442,1444],{"class":1443},"sRBoR"," \u003C",[424,1446,1447],{"class":660}," 0",[424,1449,1450],{"class":1443}," ||",[424,1452,1440],{"class":497},[424,1454,1455],{"class":1443}," >",[424,1457,1458],{"class":660}," 100",[424,1460,585],{"class":438},[424,1462,1463,1465,1467,1469,1472,1474,1477,1479,1481,1484,1486,1489,1491,1493,1495],{"class":426,"line":619},[424,1464,1164],{"class":903},[424,1466,1447],{"class":660},[424,1468,450],{"class":438},[424,1470,1471],{"class":497}," fmt",[424,1473,593],{"class":438},[424,1475,1476],{"class":434},"Errorf",[424,1478,439],{"class":438},[424,1480,602],{"class":601},[424,1482,1483],{"class":605},"percentage ",[424,1485,1318],{"class":1317},[424,1487,1488],{"class":605}," is outside 0..100",[424,1490,602],{"class":601},[424,1492,450],{"class":438},[424,1494,1440],{"class":497},[424,1496,616],{"class":438},[424,1498,1499],{"class":426,"line":625},[424,1500,1237],{"class":438},[424,1502,1503,1505,1507,1509,1511,1514],{"class":426,"line":632},[424,1504,904],{"class":903},[424,1506,1370],{"class":434},[424,1508,439],{"class":438},[424,1510,1413],{"class":497},[424,1512,1513],{"class":438},"),",[424,1515,1516],{"class":1052}," nil\n",[424,1518,1519],{"class":426,"line":645},[424,1520,622],{"class":438},[395,1522,1523],{},"To make the constructor impossible to bypass, define the type as a struct with an unexported field, in its own package, so outside code can only create values through your function.",[390,1525,1527],{"id":1526},"type-aliases","Type aliases",[395,1529,1530,1531,1534,1535,1538],{},"An ",[555,1532,1533],{},"alias"," gives an existing type a second name. It uses ",[399,1536,1537],{},"=",":",[415,1540,1542],{"className":417,"code":1541,"language":419,"meta":420,"style":420},"type Byte = uint8\n",[399,1543,1544],{"__ignoreMap":420},[424,1545,1546,1548,1551,1553],{"class":426,"line":427},[424,1547,530],{"class":430},[424,1549,1550],{"class":533}," Byte",[424,1552,657],{"class":656},[424,1554,1555],{"class":446}," uint8\n",[395,1557,1558,1561,1562,1565],{},[399,1559,1560],{},"Byte"," and ",[399,1563,1564],{},"uint8"," are the very same type, so no conversion is needed between them. Compare the two forms side by side:",[415,1567,1569],{"className":417,"code":1568,"language":419,"meta":420,"style":420},"type UserID int64        \u002F\u002F defined type: new, distinct type\ntype Identifier = int64  \u002F\u002F alias: just another name for int64\n",[399,1570,1571,1582],{"__ignoreMap":420},[424,1572,1573,1575,1577,1579],{"class":426,"line":427},[424,1574,530],{"class":430},[424,1576,534],{"class":533},[424,1578,447],{"class":446},[424,1580,1581],{"class":512},"        \u002F\u002F defined type: new, distinct type\n",[424,1583,1584,1586,1589,1591,1593],{"class":426,"line":540},[424,1585,530],{"class":430},[424,1587,1588],{"class":533}," Identifier",[424,1590,657],{"class":656},[424,1592,447],{"class":446},[424,1594,1595],{"class":512},"  \u002F\u002F alias: just another name for int64\n",[395,1597,1598],{},"An alias creates no new meaning, so it cannot protect you from mix-ups, and it cannot have its own methods. Aliases exist mainly to help with gradual refactoring, for example when moving a type to another package, and to give shorter names to long type expressions. If you want new meaning or behaviour, use a defined type.",[390,1600,1602],{"id":1601},"common-mistakes","Common mistakes",[1604,1605,1606,1613,1619,1625,1631],"ul",{},[1607,1608,1609,1612],"li",{},[555,1610,1611],{},"Expecting automatic validation."," A defined type accepts every value its underlying type does.",[1607,1614,1615,1618],{},[555,1616,1617],{},"Converting just to make the error go away."," First ask whether the mismatch points to a real bug.",[1607,1620,1621,1624],{},[555,1622,1623],{},"Defining a type for every local variable."," Types pay off at meaningful boundaries, like IDs, units, and states.",[1607,1626,1627,1630],{},[555,1628,1629],{},"Using an alias when you wanted a new type."," An alias gives you no extra safety.",[1607,1632,1633,1636],{},[555,1634,1635],{},"Comparing against raw numbers all over the code"," when named constants exist.",[390,1638,1640],{"id":1639},"practice","Practice",[395,1642,1643,1644,1561,1647,1650,1651,1654,1655,1658,1659,1661,1662,1664],{},"Create distinct ",[399,1645,1646],{},"AccountID",[399,1648,1649],{},"TransactionID"," types based on ",[399,1652,1653],{},"string",". Give each a ",[399,1656,1657],{},"Valid"," method that rejects an empty value. Write a function that accepts an ",[399,1660,1646],{},", and demonstrate that passing a ",[399,1663,1649],{}," to it does not compile.",[1666,1667,1668],"accordion",{},[1669,1670,1673,2118,2121,2127],"accordion-item",{"icon":1671,"label":1672},"i-lucide-lightbulb","Show solution",[415,1674,1676],{"className":417,"code":1675,"language":419,"meta":420,"style":420},"package main\n\nimport \"fmt\"\n\ntype AccountID string\ntype TransactionID string\n\nfunc (id AccountID) Valid() bool {\n    return id != \"\"\n}\n\nfunc (id TransactionID) Valid() bool {\n    return id != \"\"\n}\n\nfunc loadAccount(id AccountID) error {\n    if !id.Valid() {\n        return fmt.Errorf(\"account ID must not be empty\")\n    }\n    fmt.Println(\"loading account\", id)\n    return nil\n}\n\nfunc main() {\n    accountID := AccountID(\"acct-42\")\n    transactionID := TransactionID(\"tx-9\")\n\n    _ = loadAccount(accountID) \u002F\u002F prints: loading account acct-42\n\n    \u002F\u002F loadAccount(transactionID)\n    \u002F\u002F ^ does not compile: cannot use transactionID (variable of type\n    \u002F\u002F   TransactionID) as AccountID value in argument to loadAccount\n    fmt.Println(\"transaction:\", transactionID)\n\n    \u002F\u002F An empty ID is caught at run time by Valid:\n    if err := loadAccount(AccountID(\"\")); err != nil {\n        fmt.Println(\"error:\", err)\n    }\n}\n",[399,1677,1678,1686,1690,1703,1707,1717,1726,1730,1753,1765,1769,1773,1793,1803,1807,1811,1830,1847,1868,1872,1896,1903,1908,1913,1924,1945,1966,1971,1991,1996,2002,2008,2014,2039,2044,2050,2083,2108,2113],{"__ignoreMap":420},[424,1679,1680,1683],{"class":426,"line":427},[424,1681,1682],{"class":430},"package",[424,1684,1685],{"class":533}," main\n",[424,1687,1688],{"class":426,"line":540},[424,1689,629],{"emptyLinePlaceholder":628},[424,1691,1692,1695,1697,1701],{"class":426,"line":619},[424,1693,1694],{"class":903},"import",[424,1696,1167],{"class":601},[424,1698,1700],{"class":1699},"sdLNw","fmt",[424,1702,1173],{"class":601},[424,1704,1705],{"class":426,"line":625},[424,1706,629],{"emptyLinePlaceholder":628},[424,1708,1709,1711,1714],{"class":426,"line":632},[424,1710,530],{"class":430},[424,1712,1713],{"class":533}," AccountID",[424,1715,1716],{"class":446}," string\n",[424,1718,1719,1721,1724],{"class":426,"line":645},[424,1720,530],{"class":430},[424,1722,1723],{"class":533}," TransactionID",[424,1725,1716],{"class":446},[424,1727,1728],{"class":426,"line":664},[424,1729,629],{"emptyLinePlaceholder":628},[424,1731,1732,1734,1736,1739,1741,1743,1746,1748,1751],{"class":426,"line":678},[424,1733,431],{"class":430},[424,1735,880],{"class":438},[424,1737,1738],{"class":442},"id ",[424,1740,1646],{"class":533},[424,1742,465],{"class":438},[424,1744,1745],{"class":434}," Valid",[424,1747,640],{"class":438},[424,1749,1750],{"class":446}," bool",[424,1752,585],{"class":438},[424,1754,1755,1757,1759,1762],{"class":426,"line":683},[424,1756,904],{"class":903},[424,1758,613],{"class":497},[424,1760,1761],{"class":1443}," !=",[424,1763,1764],{"class":601}," \"\"\n",[424,1766,1767],{"class":426,"line":699},[424,1768,622],{"class":438},[424,1770,1771],{"class":426,"line":714},[424,1772,629],{"emptyLinePlaceholder":628},[424,1774,1775,1777,1779,1781,1783,1785,1787,1789,1791],{"class":426,"line":1240},[424,1776,431],{"class":430},[424,1778,880],{"class":438},[424,1780,1738],{"class":442},[424,1782,1649],{"class":533},[424,1784,465],{"class":438},[424,1786,1745],{"class":434},[424,1788,640],{"class":438},[424,1790,1750],{"class":446},[424,1792,585],{"class":438},[424,1794,1795,1797,1799,1801],{"class":426,"line":1252},[424,1796,904],{"class":903},[424,1798,613],{"class":497},[424,1800,1761],{"class":1443},[424,1802,1764],{"class":601},[424,1804,1805],{"class":426,"line":1257},[424,1806,622],{"class":438},[424,1808,1809],{"class":426,"line":1262},[424,1810,629],{"emptyLinePlaceholder":628},[424,1812,1813,1815,1818,1820,1822,1824,1826,1828],{"class":426,"line":1273},[424,1814,431],{"class":430},[424,1816,1817],{"class":434}," loadAccount",[424,1819,439],{"class":438},[424,1821,578],{"class":442},[424,1823,1713],{"class":533},[424,1825,465],{"class":438},[424,1827,1428],{"class":446},[424,1829,585],{"class":438},[424,1831,1832,1834,1837,1839,1841,1843,1845],{"class":426,"line":1284},[424,1833,1437],{"class":903},[424,1835,1836],{"class":1443}," !",[424,1838,578],{"class":497},[424,1840,593],{"class":438},[424,1842,1657],{"class":434},[424,1844,640],{"class":438},[424,1846,585],{"class":438},[424,1848,1849,1851,1853,1855,1857,1859,1861,1864,1866],{"class":426,"line":1303},[424,1850,1164],{"class":903},[424,1852,1471],{"class":497},[424,1854,593],{"class":438},[424,1856,1476],{"class":434},[424,1858,439],{"class":438},[424,1860,602],{"class":601},[424,1862,1863],{"class":605},"account ID must not be empty",[424,1865,602],{"class":601},[424,1867,616],{"class":438},[424,1869,1870],{"class":426,"line":1337},[424,1871,1237],{"class":438},[424,1873,1875,1877,1879,1881,1883,1885,1888,1890,1892,1894],{"class":426,"line":1874},20,[424,1876,590],{"class":497},[424,1878,593],{"class":438},[424,1880,596],{"class":434},[424,1882,439],{"class":438},[424,1884,602],{"class":601},[424,1886,1887],{"class":605},"loading account",[424,1889,602],{"class":601},[424,1891,450],{"class":438},[424,1893,613],{"class":497},[424,1895,616],{"class":438},[424,1897,1899,1901],{"class":426,"line":1898},21,[424,1900,904],{"class":903},[424,1902,1516],{"class":1052},[424,1904,1906],{"class":426,"line":1905},22,[424,1907,622],{"class":438},[424,1909,1911],{"class":426,"line":1910},23,[424,1912,629],{"emptyLinePlaceholder":628},[424,1914,1916,1918,1920,1922],{"class":426,"line":1915},24,[424,1917,431],{"class":430},[424,1919,637],{"class":434},[424,1921,640],{"class":438},[424,1923,585],{"class":438},[424,1925,1927,1930,1932,1934,1936,1938,1941,1943],{"class":426,"line":1926},25,[424,1928,1929],{"class":497},"    accountID",[424,1931,798],{"class":656},[424,1933,1713],{"class":434},[424,1935,439],{"class":438},[424,1937,602],{"class":601},[424,1939,1940],{"class":605},"acct-42",[424,1942,602],{"class":601},[424,1944,616],{"class":438},[424,1946,1948,1951,1953,1955,1957,1959,1962,1964],{"class":426,"line":1947},26,[424,1949,1950],{"class":497},"    transactionID",[424,1952,798],{"class":656},[424,1954,1723],{"class":434},[424,1956,439],{"class":438},[424,1958,602],{"class":601},[424,1960,1961],{"class":605},"tx-9",[424,1963,602],{"class":601},[424,1965,616],{"class":438},[424,1967,1969],{"class":426,"line":1968},27,[424,1970,629],{"emptyLinePlaceholder":628},[424,1972,1974,1977,1979,1981,1983,1986,1988],{"class":426,"line":1973},28,[424,1975,1976],{"class":497},"    _",[424,1978,657],{"class":656},[424,1980,1817],{"class":434},[424,1982,439],{"class":438},[424,1984,1985],{"class":497},"accountID",[424,1987,465],{"class":438},[424,1989,1990],{"class":512}," \u002F\u002F prints: loading account acct-42\n",[424,1992,1994],{"class":426,"line":1993},29,[424,1995,629],{"emptyLinePlaceholder":628},[424,1997,1999],{"class":426,"line":1998},30,[424,2000,2001],{"class":512},"    \u002F\u002F loadAccount(transactionID)\n",[424,2003,2005],{"class":426,"line":2004},31,[424,2006,2007],{"class":512},"    \u002F\u002F ^ does not compile: cannot use transactionID (variable of type\n",[424,2009,2011],{"class":426,"line":2010},32,[424,2012,2013],{"class":512},"    \u002F\u002F   TransactionID) as AccountID value in argument to loadAccount\n",[424,2015,2017,2019,2021,2023,2025,2027,2030,2032,2034,2037],{"class":426,"line":2016},33,[424,2018,590],{"class":497},[424,2020,593],{"class":438},[424,2022,596],{"class":434},[424,2024,439],{"class":438},[424,2026,602],{"class":601},[424,2028,2029],{"class":605},"transaction:",[424,2031,602],{"class":601},[424,2033,450],{"class":438},[424,2035,2036],{"class":497}," transactionID",[424,2038,616],{"class":438},[424,2040,2042],{"class":426,"line":2041},34,[424,2043,629],{"emptyLinePlaceholder":628},[424,2045,2047],{"class":426,"line":2046},35,[424,2048,2049],{"class":512},"    \u002F\u002F An empty ID is caught at run time by Valid:\n",[424,2051,2053,2055,2058,2060,2062,2064,2066,2068,2071,2074,2076,2078,2081],{"class":426,"line":2052},36,[424,2054,1437],{"class":903},[424,2056,2057],{"class":497}," err",[424,2059,798],{"class":656},[424,2061,1817],{"class":434},[424,2063,439],{"class":438},[424,2065,1646],{"class":434},[424,2067,439],{"class":438},[424,2069,2070],{"class":601},"\"\"",[424,2072,2073],{"class":438},"));",[424,2075,2057],{"class":497},[424,2077,1761],{"class":1443},[424,2079,2080],{"class":1052}," nil",[424,2082,585],{"class":438},[424,2084,2086,2089,2091,2093,2095,2097,2100,2102,2104,2106],{"class":426,"line":2085},37,[424,2087,2088],{"class":497},"        fmt",[424,2090,593],{"class":438},[424,2092,596],{"class":434},[424,2094,439],{"class":438},[424,2096,602],{"class":601},[424,2098,2099],{"class":605},"error:",[424,2101,602],{"class":601},[424,2103,450],{"class":438},[424,2105,2057],{"class":497},[424,2107,616],{"class":438},[424,2109,2111],{"class":426,"line":2110},38,[424,2112,1237],{"class":438},[424,2114,2116],{"class":426,"line":2115},39,[424,2117,622],{"class":438},[395,2119,2120],{},"Output:",[415,2122,2125],{"className":2123,"code":2124,"language":722,"meta":420},[720],"loading account acct-42\ntransaction: tx-9\nerror: account ID must not be empty\n",[399,2126,2124],{"__ignoreMap":420},[395,2128,2129,2130,2132],{},"Both types are strings underneath, but the compiler keeps their meanings apart, so the commented-out call is rejected. The ",[399,2131,1657],{}," methods check the simplest rule, emptiness. If every ID must be valid from the moment it exists, a constructor function that returns an error is the stronger design.",[390,2134,2136],{"id":2135},"summary","Summary",[395,2138,2139,2140,2143,2144,2146],{},"A defined type, written ",[399,2141,2142],{},"type UserID int64",", is a new and distinct type with the same underlying representation, so the compiler stops you from mixing it with other types by accident. Converting to and from the underlying type is always explicit. Defined types can have methods and can model enums with constants and ",[399,2145,405],{},". A type does not validate its values by itself, so use a constructor for rules, and remember that an alias gives a second name to the same type without creating anything new.",[395,2148,2149,2150,1561,2157,593],{},"Official reference: ",[2151,2152,2156],"a",{"href":2153,"rel":2154},"https:\u002F\u002Fgo.dev\u002Fref\u002Fspec#Type_declarations",[2155],"nofollow","Type declarations in the specification",[2151,2158,2161],{"href":2159,"rel":2160},"https:\u002F\u002Fgo.dev\u002Ftour\u002Fmethods\u002F1",[2155],"A Tour of Go: Methods",[2163,2164],"learning-lesson-complete",{},[2166,2167],"learning-lesson-navigation",{"next":71,"nextTitle":70,"previous":61,"previousTitle":60},[2169,2170,2171],"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 .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 .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 .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}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 .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}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 .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 .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 .sry10, html code.shiki .sry10{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E5C07B}html pre.shiki code .sp8Z5, html code.shiki .sp8Z5{--shiki-light:#B58900;--shiki-default:#89DDFF;--shiki-dark:#D19A66}html pre.shiki code .st3jj, html code.shiki .st3jj{--shiki-light:#CB4B16;--shiki-default:#F07178;--shiki-dark:#D19A66}html pre.shiki code .sk51v, html code.shiki .sk51v{--shiki-light:#CB4B16;--shiki-default:#EEFFFF;--shiki-dark:#56B6C2}html pre.shiki code .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .sdLNw, html code.shiki .sdLNw{--shiki-light:#2AA198;--shiki-default:#FFCB6B;--shiki-dark:#98C379}",{"title":420,"searchDepth":540,"depth":540,"links":2173},[2174,2175,2176,2177,2178,2179,2180,2182,2183,2184,2185,2186],{"id":392,"depth":540,"text":393},{"id":409,"depth":540,"text":410},{"id":519,"depth":540,"text":520},{"id":730,"depth":540,"text":731},{"id":815,"depth":540,"text":816},{"id":850,"depth":540,"text":851},{"id":1002,"depth":540,"text":2181},"Enum-like constants with iota",{"id":1354,"depth":540,"text":1355},{"id":1526,"depth":540,"text":1527},{"id":1601,"depth":540,"text":1602},{"id":1639,"depth":540,"text":1640},{"id":2135,"depth":540,"text":2136},"Give values domain meaning with defined types, build enum-like constants with iota, and learn how an alias differs.","md",null,{},{"icon":68},{"description":2193,"title":65},"Learn Go defined types, underlying types, explicit conversions, methods on defined types, enum-like constants with iota, validation constructors, and type aliases.","mhKGKQd-pXHryZaFGo8f4cWtr2tWv-pre6DtiWmMo3M",[2196,2198],{"title":60,"path":61,"stem":62,"description":2197,"icon":63,"children":-1},"Return, wrap, inspect, and design errors so that failures are visible and useful.",{"title":70,"path":71,"stem":72,"description":2199,"icon":73,"children":-1},"Write one function or data structure that works across many types, using type parameters and constraints.",1794118097521]