[{"data":1,"prerenderedAt":2396},["ShallowReactive",2],{"navigation_docs":3,"-fundamentals-arrays-and-slices":377,"-fundamentals-arrays-and-slices-surround":2391},[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":105,"body":379,"description":2383,"extension":2384,"links":2385,"meta":2386,"navigation":2387,"path":106,"seo":2388,"stem":107,"__hash__":2390},"docs\u002F2.fundamentals\u002F8.arrays-and-slices.md",{"type":380,"value":381,"toc":2365},"minimark",[382,389,394,398,402,545,556,562,646,649,653,656,774,786,794,797,801,875,878,885,995,1010,1117,1120,1124,1138,1256,1265,1269,1276,1369,1379,1384,1477,1483,1487,1610,1616,1626,1633,1730,1739,1743,1749,1858,1865,1869,1904,1924,1928,1989,1993,2003,2319,2323,2340,2355,2358,2361],[383,384],"learning-lesson-meta",{"description":385,"difficulty":386,"prerequisites":387,"time":388},"Use arrays and slices, and learn when two slices share the same underlying data.","Beginner","Pointers, Control Statements","40 minutes",[390,391,393],"h2",{"id":392},"what-you-will-learn","What you will learn",[395,396,397],"p",{},"Arrays and slices both hold an ordered list of values of one type. You will use slices almost all the time, but arrays explain how slices work. This lesson covers creating, indexing, appending, slicing, copying, and looping, and ends with the one behaviour that surprises almost everyone: two slices can share the same underlying data.",[390,399,401],{"id":400},"arrays-have-a-fixed-length","Arrays have a fixed length",[403,404,409],"pre",{"className":405,"code":406,"language":407,"meta":408,"style":408},"language-go shiki shiki-themes solarized-light material-theme one-dark-pro","scores := [3]int{90, 82, 97}\n\nfmt.Println(scores[0])   \u002F\u002F 90\nfmt.Println(len(scores)) \u002F\u002F 3\n\nscores[1] = 85\n","go","",[410,411,412,460,467,498,522,527],"code",{"__ignoreMap":408},[413,414,417,421,425,429,433,436,440,443,446,449,452,454,457],"span",{"class":415,"line":416},"line",1,[413,418,420],{"class":419},"s45-6","scores",[413,422,424],{"class":423},"su2KV"," :=",[413,426,428],{"class":427},"shFME"," [",[413,430,432],{"class":431},"sAJzr","3",[413,434,435],{"class":427},"]",[413,437,439],{"class":438},"sF6sA","int",[413,441,442],{"class":427},"{",[413,444,445],{"class":431},"90",[413,447,448],{"class":427},",",[413,450,451],{"class":431}," 82",[413,453,448],{"class":427},[413,455,456],{"class":431}," 97",[413,458,459],{"class":427},"}\n",[413,461,463],{"class":415,"line":462},2,[413,464,466],{"emptyLinePlaceholder":465},true,"\n",[413,468,470,473,476,480,483,485,488,491,494],{"class":415,"line":469},3,[413,471,472],{"class":419},"fmt",[413,474,475],{"class":427},".",[413,477,479],{"class":478},"sisdC","Println",[413,481,482],{"class":427},"(",[413,484,420],{"class":419},[413,486,487],{"class":427},"[",[413,489,490],{"class":431},"0",[413,492,493],{"class":427},"])",[413,495,497],{"class":496},"sJsHy","   \u002F\u002F 90\n",[413,499,501,503,505,507,509,512,514,516,519],{"class":415,"line":500},4,[413,502,472],{"class":419},[413,504,475],{"class":427},[413,506,479],{"class":478},[413,508,482],{"class":427},[413,510,511],{"class":478},"len",[413,513,482],{"class":427},[413,515,420],{"class":419},[413,517,518],{"class":427},"))",[413,520,521],{"class":496}," \u002F\u002F 3\n",[413,523,525],{"class":415,"line":524},5,[413,526,466],{"emptyLinePlaceholder":465},[413,528,530,532,534,537,539,542],{"class":415,"line":529},6,[413,531,420],{"class":419},[413,533,487],{"class":427},[413,535,536],{"class":431},"1",[413,538,435],{"class":427},[413,540,541],{"class":423}," =",[413,543,544],{"class":431}," 85\n",[395,546,547,548,551,552,555],{},"The length is part of the type, so ",[410,549,550],{},"[3]int"," and ",[410,553,554],{},"[4]int"," are different types, and an array can never grow. Indexes start at 0, and an index outside the array is a compile error for constants or a runtime panic otherwise.",[395,557,558,559,561],{},"An array is a value, like an ",[410,560,439],{},". Assigning it or passing it to a function copies every element:",[403,563,565],{"className":405,"code":564,"language":407,"meta":408,"style":408},"a := [3]int{1, 2, 3}\nb := a\nb[0] = 99\nfmt.Println(a, b) \u002F\u002F [1 2 3] [99 2 3]\n",[410,566,567,598,608,623],{"__ignoreMap":408},[413,568,569,572,574,576,578,580,582,584,586,588,591,593,596],{"class":415,"line":416},[413,570,571],{"class":419},"a",[413,573,424],{"class":423},[413,575,428],{"class":427},[413,577,432],{"class":431},[413,579,435],{"class":427},[413,581,439],{"class":438},[413,583,442],{"class":427},[413,585,536],{"class":431},[413,587,448],{"class":427},[413,589,590],{"class":431}," 2",[413,592,448],{"class":427},[413,594,595],{"class":431}," 3",[413,597,459],{"class":427},[413,599,600,603,605],{"class":415,"line":462},[413,601,602],{"class":419},"b",[413,604,424],{"class":423},[413,606,607],{"class":419}," a\n",[413,609,610,612,614,616,618,620],{"class":415,"line":469},[413,611,602],{"class":419},[413,613,487],{"class":427},[413,615,490],{"class":431},[413,617,435],{"class":427},[413,619,541],{"class":423},[413,621,622],{"class":431}," 99\n",[413,624,625,627,629,631,633,635,637,640,643],{"class":415,"line":500},[413,626,472],{"class":419},[413,628,475],{"class":427},[413,630,479],{"class":478},[413,632,482],{"class":427},[413,634,571],{"class":419},[413,636,448],{"class":427},[413,638,639],{"class":419}," b",[413,641,642],{"class":427},")",[413,644,645],{"class":496}," \u002F\u002F [1 2 3] [99 2 3]\n",[395,647,648],{},"Because of the fixed length and the copying, arrays are used less often than slices. Reach for an array when the size is truly part of the problem, such as a 3-component colour or a fixed-size hash.",[390,650,652],{"id":651},"slices-are-flexible","Slices are flexible",[395,654,655],{},"A slice looks like an array without the length:",[403,657,659],{"className":405,"code":658,"language":407,"meta":408,"style":408},"names := []string{\"Ada\", \"Linus\", \"Grace\"}\nnames = append(names, \"Rob\")\n\nfmt.Println(names)      \u002F\u002F [Ada Linus Grace Rob]\nfmt.Println(len(names)) \u002F\u002F 4\n",[410,660,661,707,732,736,753],{"__ignoreMap":408},[413,662,663,666,668,671,674,676,680,684,686,688,691,694,696,698,700,703,705],{"class":415,"line":416},[413,664,665],{"class":419},"names",[413,667,424],{"class":423},[413,669,670],{"class":427}," []",[413,672,673],{"class":438},"string",[413,675,442],{"class":427},[413,677,679],{"class":678},"sED5a","\"",[413,681,683],{"class":682},"sVazx","Ada",[413,685,679],{"class":678},[413,687,448],{"class":427},[413,689,690],{"class":678}," \"",[413,692,693],{"class":682},"Linus",[413,695,679],{"class":678},[413,697,448],{"class":427},[413,699,690],{"class":678},[413,701,702],{"class":682},"Grace",[413,704,679],{"class":678},[413,706,459],{"class":427},[413,708,709,711,713,716,718,720,722,724,727,729],{"class":415,"line":462},[413,710,665],{"class":419},[413,712,541],{"class":423},[413,714,715],{"class":478}," append",[413,717,482],{"class":427},[413,719,665],{"class":419},[413,721,448],{"class":427},[413,723,690],{"class":678},[413,725,726],{"class":682},"Rob",[413,728,679],{"class":678},[413,730,731],{"class":427},")\n",[413,733,734],{"class":415,"line":469},[413,735,466],{"emptyLinePlaceholder":465},[413,737,738,740,742,744,746,748,750],{"class":415,"line":500},[413,739,472],{"class":419},[413,741,475],{"class":427},[413,743,479],{"class":478},[413,745,482],{"class":427},[413,747,665],{"class":419},[413,749,642],{"class":427},[413,751,752],{"class":496},"      \u002F\u002F [Ada Linus Grace Rob]\n",[413,754,755,757,759,761,763,765,767,769,771],{"class":415,"line":524},[413,756,472],{"class":419},[413,758,475],{"class":427},[413,760,479],{"class":478},[413,762,482],{"class":427},[413,764,511],{"class":478},[413,766,482],{"class":427},[413,768,665],{"class":419},[413,770,518],{"class":427},[413,772,773],{"class":496}," \u002F\u002F 4\n",[395,775,776,777,781,782,785],{},"A slice has three pieces of information: where its elements are stored, its ",[778,779,780],"strong",{},"length"," (how many elements it currently has), and its ",[778,783,784],{},"capacity"," (how many it can hold before Go has to find more room). Conceptually:",[403,787,792],{"className":788,"code":790,"language":791,"meta":408},[789],"language-text","names  ──►  [ \"Ada\" | \"Linus\" | \"Grace\" | \"Rob\" | _ | _ ]\n            len = 4                         cap = 6\n","text",[410,793,790],{"__ignoreMap":408},[395,795,796],{},"The elements live in an underlying array, and the slice is a small window onto it. This is why slices are cheap to pass around, and also why they can share data.",[390,798,800],{"id":799},"creating-slices","Creating slices",[403,802,804],{"className":405,"code":803,"language":407,"meta":408,"style":408},"empty := []int{}               \u002F\u002F length 0\nitems := make([]string, 3)     \u002F\u002F length 3, capacity 3, filled with \"\"\nqueue := make([]string, 0, 10) \u002F\u002F length 0, capacity 10\n",[410,805,806,823,847],{"__ignoreMap":408},[413,807,808,811,813,815,817,820],{"class":415,"line":416},[413,809,810],{"class":419},"empty",[413,812,424],{"class":423},[413,814,670],{"class":427},[413,816,439],{"class":438},[413,818,819],{"class":427},"{}",[413,821,822],{"class":496},"               \u002F\u002F length 0\n",[413,824,825,828,830,833,836,838,840,842,844],{"class":415,"line":462},[413,826,827],{"class":419},"items",[413,829,424],{"class":423},[413,831,832],{"class":478}," make",[413,834,835],{"class":427},"([]",[413,837,673],{"class":438},[413,839,448],{"class":427},[413,841,595],{"class":431},[413,843,642],{"class":427},[413,845,846],{"class":496},"     \u002F\u002F length 3, capacity 3, filled with \"\"\n",[413,848,849,852,854,856,858,860,862,865,867,870,872],{"class":415,"line":469},[413,850,851],{"class":419},"queue",[413,853,424],{"class":423},[413,855,832],{"class":478},[413,857,835],{"class":427},[413,859,673],{"class":438},[413,861,448],{"class":427},[413,863,864],{"class":431}," 0",[413,866,448],{"class":427},[413,868,869],{"class":431}," 10",[413,871,642],{"class":427},[413,873,874],{"class":496}," \u002F\u002F length 0, capacity 10\n",[395,876,877],{},"The second form is useful when you know roughly how many items you will add. Reserving capacity up front avoids repeated allocation while keeping the slice logically empty.",[390,879,881,882],{"id":880},"growing-with-append","Growing with ",[410,883,884],{},"append",[403,886,888],{"className":405,"code":887,"language":407,"meta":408,"style":408},"values := []int{1, 2, 3}\nvalues = append(values, 4)\nvalues = append(values, 5, 6)\nvalues = append(values, []int{7, 8}...)\n",[410,889,890,915,934,958],{"__ignoreMap":408},[413,891,892,895,897,899,901,903,905,907,909,911,913],{"class":415,"line":416},[413,893,894],{"class":419},"values",[413,896,424],{"class":423},[413,898,670],{"class":427},[413,900,439],{"class":438},[413,902,442],{"class":427},[413,904,536],{"class":431},[413,906,448],{"class":427},[413,908,590],{"class":431},[413,910,448],{"class":427},[413,912,595],{"class":431},[413,914,459],{"class":427},[413,916,917,919,921,923,925,927,929,932],{"class":415,"line":462},[413,918,894],{"class":419},[413,920,541],{"class":423},[413,922,715],{"class":478},[413,924,482],{"class":427},[413,926,894],{"class":419},[413,928,448],{"class":427},[413,930,931],{"class":431}," 4",[413,933,731],{"class":427},[413,935,936,938,940,942,944,946,948,951,953,956],{"class":415,"line":469},[413,937,894],{"class":419},[413,939,541],{"class":423},[413,941,715],{"class":478},[413,943,482],{"class":427},[413,945,894],{"class":419},[413,947,448],{"class":427},[413,949,950],{"class":431}," 5",[413,952,448],{"class":427},[413,954,955],{"class":431}," 6",[413,957,731],{"class":427},[413,959,960,962,964,966,968,970,972,974,976,978,981,983,986,989,993],{"class":415,"line":500},[413,961,894],{"class":419},[413,963,541],{"class":423},[413,965,715],{"class":478},[413,967,482],{"class":427},[413,969,894],{"class":419},[413,971,448],{"class":427},[413,973,670],{"class":427},[413,975,439],{"class":438},[413,977,442],{"class":427},[413,979,980],{"class":431},"7",[413,982,448],{"class":427},[413,984,985],{"class":431}," 8",[413,987,988],{"class":427},"}",[413,990,992],{"class":991},"sN42N","...",[413,994,731],{"class":427},[395,996,997,999,1000,1002,1003,1006,1007,1009],{},[410,998,884],{}," adds one or more elements, and ",[410,1001,992],{}," spreads another slice. ",[778,1004,1005],{},"Always assign the result back",", because ",[410,1008,884],{}," may return a different slice. If the underlying array has room, Go reuses it. If not, Go allocates a bigger array, copies the elements across, and returns a slice that points to the new one. You can watch capacity grow:",[403,1011,1013],{"className":405,"code":1012,"language":407,"meta":408,"style":408},"var s []int\nfor i := 0; i \u003C 5; i++ {\n    s = append(s, i)\n    fmt.Println(len(s), cap(s))\n}\n",[410,1014,1015,1029,1063,1083,1113],{"__ignoreMap":408},[413,1016,1017,1021,1024,1026],{"class":415,"line":416},[413,1018,1020],{"class":1019},"sEOdo","var",[413,1022,1023],{"class":419}," s",[413,1025,670],{"class":427},[413,1027,1028],{"class":438},"int\n",[413,1030,1031,1035,1038,1040,1042,1045,1047,1051,1053,1055,1057,1060],{"class":415,"line":462},[413,1032,1034],{"class":1033},"s7Bcg","for",[413,1036,1037],{"class":419}," i",[413,1039,424],{"class":423},[413,1041,864],{"class":431},[413,1043,1044],{"class":427},";",[413,1046,1037],{"class":419},[413,1048,1050],{"class":1049},"sRBoR"," \u003C",[413,1052,950],{"class":431},[413,1054,1044],{"class":427},[413,1056,1037],{"class":419},[413,1058,1059],{"class":1049},"++",[413,1061,1062],{"class":427}," {\n",[413,1064,1065,1068,1070,1072,1074,1077,1079,1081],{"class":415,"line":469},[413,1066,1067],{"class":419},"    s",[413,1069,541],{"class":423},[413,1071,715],{"class":478},[413,1073,482],{"class":427},[413,1075,1076],{"class":419},"s",[413,1078,448],{"class":427},[413,1080,1037],{"class":419},[413,1082,731],{"class":427},[413,1084,1085,1088,1090,1092,1094,1096,1098,1100,1103,1106,1108,1110],{"class":415,"line":500},[413,1086,1087],{"class":419},"    fmt",[413,1089,475],{"class":427},[413,1091,479],{"class":478},[413,1093,482],{"class":427},[413,1095,511],{"class":478},[413,1097,482],{"class":427},[413,1099,1076],{"class":419},[413,1101,1102],{"class":427},"),",[413,1104,1105],{"class":478}," cap",[413,1107,482],{"class":427},[413,1109,1076],{"class":419},[413,1111,1112],{"class":427},"))\n",[413,1114,1115],{"class":415,"line":524},[413,1116,459],{"class":427},[395,1118,1119],{},"The capacity jumps in steps rather than by one each time (for example 1, 2, 4, 4, 8), though the exact numbers are an implementation detail you should not depend on.",[390,1121,1123],{"id":1122},"slicing-a-slice","Slicing a slice",[395,1125,1126,1129,1130,1133,1134,1137],{},[410,1127,1128],{},"slice[low:high]"," creates a new slice that includes ",[410,1131,1132],{},"low"," and excludes ",[410,1135,1136],{},"high",":",[403,1139,1141],{"className":405,"code":1140,"language":407,"meta":408,"style":408},"numbers := []int{10, 20, 30, 40, 50}\n\nfmt.Println(numbers[1:4]) \u002F\u002F [20 30 40]\nfmt.Println(numbers[:2])  \u002F\u002F [10 20]\nfmt.Println(numbers[3:])  \u002F\u002F [40 50]\n",[410,1142,1143,1181,1185,1211,1234],{"__ignoreMap":408},[413,1144,1145,1148,1150,1152,1154,1156,1159,1161,1164,1166,1169,1171,1174,1176,1179],{"class":415,"line":416},[413,1146,1147],{"class":419},"numbers",[413,1149,424],{"class":423},[413,1151,670],{"class":427},[413,1153,439],{"class":438},[413,1155,442],{"class":427},[413,1157,1158],{"class":431},"10",[413,1160,448],{"class":427},[413,1162,1163],{"class":431}," 20",[413,1165,448],{"class":427},[413,1167,1168],{"class":431}," 30",[413,1170,448],{"class":427},[413,1172,1173],{"class":431}," 40",[413,1175,448],{"class":427},[413,1177,1178],{"class":431}," 50",[413,1180,459],{"class":427},[413,1182,1183],{"class":415,"line":462},[413,1184,466],{"emptyLinePlaceholder":465},[413,1186,1187,1189,1191,1193,1195,1197,1199,1201,1203,1206,1208],{"class":415,"line":469},[413,1188,472],{"class":419},[413,1190,475],{"class":427},[413,1192,479],{"class":478},[413,1194,482],{"class":427},[413,1196,1147],{"class":419},[413,1198,487],{"class":427},[413,1200,536],{"class":431},[413,1202,1137],{"class":427},[413,1204,1205],{"class":431},"4",[413,1207,493],{"class":427},[413,1209,1210],{"class":496}," \u002F\u002F [20 30 40]\n",[413,1212,1213,1215,1217,1219,1221,1223,1226,1229,1231],{"class":415,"line":500},[413,1214,472],{"class":419},[413,1216,475],{"class":427},[413,1218,479],{"class":478},[413,1220,482],{"class":427},[413,1222,1147],{"class":419},[413,1224,1225],{"class":427},"[:",[413,1227,1228],{"class":431},"2",[413,1230,493],{"class":427},[413,1232,1233],{"class":496},"  \u002F\u002F [10 20]\n",[413,1235,1236,1238,1240,1242,1244,1246,1248,1250,1253],{"class":415,"line":524},[413,1237,472],{"class":419},[413,1239,475],{"class":427},[413,1241,479],{"class":478},[413,1243,482],{"class":427},[413,1245,1147],{"class":419},[413,1247,487],{"class":427},[413,1249,432],{"class":431},[413,1251,1252],{"class":427},":])",[413,1254,1255],{"class":496},"  \u002F\u002F [40 50]\n",[395,1257,1258,1259,1261,1262,1264],{},"Leaving out ",[410,1260,1132],{}," means \"from the start\" and leaving out ",[410,1263,1136],{}," means \"to the end\".",[390,1266,1268],{"id":1267},"the-shared-array-trap","The shared array trap",[395,1270,1271,1272,1275],{},"A slice created by slicing does ",[778,1273,1274],{},"not"," copy the data. It is another window onto the same underlying array:",[403,1277,1279],{"className":405,"code":1278,"language":407,"meta":408,"style":408},"numbers := []int{10, 20, 30, 40, 50}\nmiddle := numbers[1:4]\n\nmiddle[0] = 99\nfmt.Println(numbers) \u002F\u002F [10 99 30 40 50]\n",[410,1280,1281,1313,1334,1338,1352],{"__ignoreMap":408},[413,1282,1283,1285,1287,1289,1291,1293,1295,1297,1299,1301,1303,1305,1307,1309,1311],{"class":415,"line":416},[413,1284,1147],{"class":419},[413,1286,424],{"class":423},[413,1288,670],{"class":427},[413,1290,439],{"class":438},[413,1292,442],{"class":427},[413,1294,1158],{"class":431},[413,1296,448],{"class":427},[413,1298,1163],{"class":431},[413,1300,448],{"class":427},[413,1302,1168],{"class":431},[413,1304,448],{"class":427},[413,1306,1173],{"class":431},[413,1308,448],{"class":427},[413,1310,1178],{"class":431},[413,1312,459],{"class":427},[413,1314,1315,1318,1320,1323,1325,1327,1329,1331],{"class":415,"line":462},[413,1316,1317],{"class":419},"middle",[413,1319,424],{"class":423},[413,1321,1322],{"class":419}," numbers",[413,1324,487],{"class":427},[413,1326,536],{"class":431},[413,1328,1137],{"class":427},[413,1330,1205],{"class":431},[413,1332,1333],{"class":427},"]\n",[413,1335,1336],{"class":415,"line":469},[413,1337,466],{"emptyLinePlaceholder":465},[413,1339,1340,1342,1344,1346,1348,1350],{"class":415,"line":500},[413,1341,1317],{"class":419},[413,1343,487],{"class":427},[413,1345,490],{"class":431},[413,1347,435],{"class":427},[413,1349,541],{"class":423},[413,1351,622],{"class":431},[413,1353,1354,1356,1358,1360,1362,1364,1366],{"class":415,"line":524},[413,1355,472],{"class":419},[413,1357,475],{"class":427},[413,1359,479],{"class":478},[413,1361,482],{"class":427},[413,1363,1147],{"class":419},[413,1365,642],{"class":427},[413,1367,1368],{"class":496}," \u002F\u002F [10 99 30 40 50]\n",[395,1370,1371,1372,1375,1376,1378],{},"Changing ",[410,1373,1374],{},"middle[0]"," changed ",[410,1377,1147],{},", because both view the same storage. This is efficient, but it surprises people who expect a copy.",[395,1380,1381,1383],{},[410,1382,884],{}," can cause an even subtler version. If a slice has spare capacity, appending writes into the shared array and can overwrite data another slice still sees:",[403,1385,1387],{"className":405,"code":1386,"language":407,"meta":408,"style":408},"base := []int{1, 2, 3, 4}\npart := base[:2]          \u002F\u002F [1 2], with capacity 4\n\npart = append(part, 100)\nfmt.Println(base) \u002F\u002F [1 2 100 4], base was changed!\n",[410,1388,1389,1418,1437,1441,1460],{"__ignoreMap":408},[413,1390,1391,1394,1396,1398,1400,1402,1404,1406,1408,1410,1412,1414,1416],{"class":415,"line":416},[413,1392,1393],{"class":419},"base",[413,1395,424],{"class":423},[413,1397,670],{"class":427},[413,1399,439],{"class":438},[413,1401,442],{"class":427},[413,1403,536],{"class":431},[413,1405,448],{"class":427},[413,1407,590],{"class":431},[413,1409,448],{"class":427},[413,1411,595],{"class":431},[413,1413,448],{"class":427},[413,1415,931],{"class":431},[413,1417,459],{"class":427},[413,1419,1420,1423,1425,1428,1430,1432,1434],{"class":415,"line":462},[413,1421,1422],{"class":419},"part",[413,1424,424],{"class":423},[413,1426,1427],{"class":419}," base",[413,1429,1225],{"class":427},[413,1431,1228],{"class":431},[413,1433,435],{"class":427},[413,1435,1436],{"class":496},"          \u002F\u002F [1 2], with capacity 4\n",[413,1438,1439],{"class":415,"line":469},[413,1440,466],{"emptyLinePlaceholder":465},[413,1442,1443,1445,1447,1449,1451,1453,1455,1458],{"class":415,"line":500},[413,1444,1422],{"class":419},[413,1446,541],{"class":423},[413,1448,715],{"class":478},[413,1450,482],{"class":427},[413,1452,1422],{"class":419},[413,1454,448],{"class":427},[413,1456,1457],{"class":431}," 100",[413,1459,731],{"class":427},[413,1461,1462,1464,1466,1468,1470,1472,1474],{"class":415,"line":524},[413,1463,472],{"class":419},[413,1465,475],{"class":427},[413,1467,479],{"class":478},[413,1469,482],{"class":427},[413,1471,1393],{"class":419},[413,1473,642],{"class":427},[413,1475,1476],{"class":496}," \u002F\u002F [1 2 100 4], base was changed!\n",[395,1478,1479,1480],{},"The rule is: ",[778,1481,1482],{},"if you need independence, make a copy.",[390,1484,1486],{"id":1485},"copying-a-slice","Copying a slice",[403,1488,1490],{"className":405,"code":1489,"language":407,"meta":408,"style":408},"original := []int{1, 2, 3}\nclone := make([]int, len(original))\ncopy(clone, original)\n\nclone[0] = 99\nfmt.Println(original) \u002F\u002F [1 2 3]\nfmt.Println(clone)    \u002F\u002F [99 2 3]\n",[410,1491,1492,1517,1541,1557,1561,1575,1592],{"__ignoreMap":408},[413,1493,1494,1497,1499,1501,1503,1505,1507,1509,1511,1513,1515],{"class":415,"line":416},[413,1495,1496],{"class":419},"original",[413,1498,424],{"class":423},[413,1500,670],{"class":427},[413,1502,439],{"class":438},[413,1504,442],{"class":427},[413,1506,536],{"class":431},[413,1508,448],{"class":427},[413,1510,590],{"class":431},[413,1512,448],{"class":427},[413,1514,595],{"class":431},[413,1516,459],{"class":427},[413,1518,1519,1522,1524,1526,1528,1530,1532,1535,1537,1539],{"class":415,"line":462},[413,1520,1521],{"class":419},"clone",[413,1523,424],{"class":423},[413,1525,832],{"class":478},[413,1527,835],{"class":427},[413,1529,439],{"class":438},[413,1531,448],{"class":427},[413,1533,1534],{"class":478}," len",[413,1536,482],{"class":427},[413,1538,1496],{"class":419},[413,1540,1112],{"class":427},[413,1542,1543,1546,1548,1550,1552,1555],{"class":415,"line":469},[413,1544,1545],{"class":478},"copy",[413,1547,482],{"class":427},[413,1549,1521],{"class":419},[413,1551,448],{"class":427},[413,1553,1554],{"class":419}," original",[413,1556,731],{"class":427},[413,1558,1559],{"class":415,"line":500},[413,1560,466],{"emptyLinePlaceholder":465},[413,1562,1563,1565,1567,1569,1571,1573],{"class":415,"line":524},[413,1564,1521],{"class":419},[413,1566,487],{"class":427},[413,1568,490],{"class":431},[413,1570,435],{"class":427},[413,1572,541],{"class":423},[413,1574,622],{"class":431},[413,1576,1577,1579,1581,1583,1585,1587,1589],{"class":415,"line":529},[413,1578,472],{"class":419},[413,1580,475],{"class":427},[413,1582,479],{"class":478},[413,1584,482],{"class":427},[413,1586,1496],{"class":419},[413,1588,642],{"class":427},[413,1590,1591],{"class":496}," \u002F\u002F [1 2 3]\n",[413,1593,1595,1597,1599,1601,1603,1605,1607],{"class":415,"line":1594},7,[413,1596,472],{"class":419},[413,1598,475],{"class":427},[413,1600,479],{"class":478},[413,1602,482],{"class":427},[413,1604,1521],{"class":419},[413,1606,642],{"class":427},[413,1608,1609],{"class":496},"    \u002F\u002F [99 2 3]\n",[395,1611,1612,1615],{},[410,1613,1614],{},"copy(destination, source)"," copies as many elements as fit and returns how many it copied. The destination must already have the right length.",[395,1617,1618,1619,1622,1623,475],{},"The standard library's ",[410,1620,1621],{},"slices"," package offers a shorter route: ",[410,1624,1625],{},"clone := slices.Clone(original)",[390,1627,1629,1630],{"id":1628},"looping-with-range","Looping with ",[410,1631,1632],{},"range",[403,1634,1636],{"className":405,"code":1635,"language":407,"meta":408,"style":408},"for index, value := range names {\n    fmt.Println(index, value)\n}\n\nfor _, name := range names { \u002F\u002F ignore the index\n    fmt.Println(name)\n}\n",[410,1637,1638,1660,1679,1683,1687,1711,1726],{"__ignoreMap":408},[413,1639,1640,1642,1645,1647,1650,1652,1655,1658],{"class":415,"line":416},[413,1641,1034],{"class":1033},[413,1643,1644],{"class":419}," index",[413,1646,448],{"class":427},[413,1648,1649],{"class":419}," value",[413,1651,424],{"class":423},[413,1653,1654],{"class":1033}," range",[413,1656,1657],{"class":419}," names",[413,1659,1062],{"class":427},[413,1661,1662,1664,1666,1668,1670,1673,1675,1677],{"class":415,"line":462},[413,1663,1087],{"class":419},[413,1665,475],{"class":427},[413,1667,479],{"class":478},[413,1669,482],{"class":427},[413,1671,1672],{"class":419},"index",[413,1674,448],{"class":427},[413,1676,1649],{"class":419},[413,1678,731],{"class":427},[413,1680,1681],{"class":415,"line":469},[413,1682,459],{"class":427},[413,1684,1685],{"class":415,"line":500},[413,1686,466],{"emptyLinePlaceholder":465},[413,1688,1689,1691,1694,1696,1699,1701,1703,1705,1708],{"class":415,"line":524},[413,1690,1034],{"class":1033},[413,1692,1693],{"class":419}," _",[413,1695,448],{"class":427},[413,1697,1698],{"class":419}," name",[413,1700,424],{"class":423},[413,1702,1654],{"class":1033},[413,1704,1657],{"class":419},[413,1706,1707],{"class":427}," {",[413,1709,1710],{"class":496}," \u002F\u002F ignore the index\n",[413,1712,1713,1715,1717,1719,1721,1724],{"class":415,"line":529},[413,1714,1087],{"class":419},[413,1716,475],{"class":427},[413,1718,479],{"class":478},[413,1720,482],{"class":427},[413,1722,1723],{"class":419},"name",[413,1725,731],{"class":427},[413,1727,1728],{"class":415,"line":1594},[413,1729,459],{"class":427},[395,1731,1732,1735,1736,475],{},[410,1733,1734],{},"value"," is a copy of each element, so assigning to it does not change the slice. To modify elements, use the index: ",[410,1737,1738],{},"names[i] = strings.ToUpper(names[i])",[390,1740,1742],{"id":1741},"useful-helpers","Useful helpers",[395,1744,1745,1746,1748],{},"The ",[410,1747,1621],{}," package (standard library, Go 1.21+) covers common jobs:",[403,1750,1752],{"className":405,"code":1751,"language":407,"meta":408,"style":408},"nums := []int{3, 1, 2}\nslices.Sort(nums)                    \u002F\u002F [1 2 3]\nfmt.Println(slices.Contains(nums, 2)) \u002F\u002F true\nfmt.Println(slices.Index(nums, 3))    \u002F\u002F 2\n",[410,1753,1754,1780,1798,1828],{"__ignoreMap":408},[413,1755,1756,1759,1761,1763,1765,1767,1769,1771,1774,1776,1778],{"class":415,"line":416},[413,1757,1758],{"class":419},"nums",[413,1760,424],{"class":423},[413,1762,670],{"class":427},[413,1764,439],{"class":438},[413,1766,442],{"class":427},[413,1768,432],{"class":431},[413,1770,448],{"class":427},[413,1772,1773],{"class":431}," 1",[413,1775,448],{"class":427},[413,1777,590],{"class":431},[413,1779,459],{"class":427},[413,1781,1782,1784,1786,1789,1791,1793,1795],{"class":415,"line":462},[413,1783,1621],{"class":419},[413,1785,475],{"class":427},[413,1787,1788],{"class":478},"Sort",[413,1790,482],{"class":427},[413,1792,1758],{"class":419},[413,1794,642],{"class":427},[413,1796,1797],{"class":496},"                    \u002F\u002F [1 2 3]\n",[413,1799,1800,1802,1804,1806,1808,1810,1812,1815,1817,1819,1821,1823,1825],{"class":415,"line":469},[413,1801,472],{"class":419},[413,1803,475],{"class":427},[413,1805,479],{"class":478},[413,1807,482],{"class":427},[413,1809,1621],{"class":419},[413,1811,475],{"class":427},[413,1813,1814],{"class":478},"Contains",[413,1816,482],{"class":427},[413,1818,1758],{"class":419},[413,1820,448],{"class":427},[413,1822,590],{"class":431},[413,1824,518],{"class":427},[413,1826,1827],{"class":496}," \u002F\u002F true\n",[413,1829,1830,1832,1834,1836,1838,1840,1842,1845,1847,1849,1851,1853,1855],{"class":415,"line":500},[413,1831,472],{"class":419},[413,1833,475],{"class":427},[413,1835,479],{"class":478},[413,1837,482],{"class":427},[413,1839,1621],{"class":419},[413,1841,475],{"class":427},[413,1843,1844],{"class":478},"Index",[413,1846,482],{"class":427},[413,1848,1758],{"class":419},[413,1850,448],{"class":427},[413,1852,595],{"class":431},[413,1854,518],{"class":427},[413,1856,1857],{"class":496},"    \u002F\u002F 2\n",[395,1859,1860,1861,1864],{},"Removing an element does not need a special function in the language: ",[410,1862,1863],{},"slices.Delete(nums, 1, 2)"," removes index 1.",[390,1866,1868],{"id":1867},"nil-and-empty-slices","Nil and empty slices",[403,1870,1872],{"className":405,"code":1871,"language":407,"meta":408,"style":408},"var nilSlice []int   \u002F\u002F nil\nemptySlice := []int{} \u002F\u002F not nil, but empty\n",[410,1873,1874,1888],{"__ignoreMap":408},[413,1875,1876,1878,1881,1883,1885],{"class":415,"line":416},[413,1877,1020],{"class":1019},[413,1879,1880],{"class":419}," nilSlice",[413,1882,670],{"class":427},[413,1884,439],{"class":438},[413,1886,1887],{"class":496},"   \u002F\u002F nil\n",[413,1889,1890,1893,1895,1897,1899,1901],{"class":415,"line":462},[413,1891,1892],{"class":419},"emptySlice",[413,1894,424],{"class":423},[413,1896,670],{"class":427},[413,1898,439],{"class":438},[413,1900,819],{"class":427},[413,1902,1903],{"class":496}," \u002F\u002F not nil, but empty\n",[395,1905,1906,1907,551,1909,1911,1912,1915,1916,1919,1920,1923],{},"Both have length 0 and both work with ",[410,1908,884],{},[410,1910,1632],{},". They differ only when compared to ",[410,1913,1914],{},"nil",", or when encoded as JSON (",[410,1917,1918],{},"null"," versus ",[410,1921,1922],{},"[]","). Most code can treat them the same.",[390,1925,1927],{"id":1926},"common-mistakes","Common mistakes",[1929,1930,1931,1945,1958,1971,1977],"ul",{},[1932,1933,1934,1940,1941,1944],"li",{},[778,1935,1936,1937,1939],{},"Not using ",[410,1938,884],{},"'s result."," Writing ",[410,1942,1943],{},"append(values, 4)"," alone does nothing useful.",[1932,1946,1947,1950,1951,1954,1955,475],{},[778,1948,1949],{},"Indexing out of range."," ",[410,1952,1953],{},"values[len(values)]"," panics with ",[410,1956,1957],{},"index out of range",[1932,1959,1960,1963,1964,1966,1967,1970],{},[778,1961,1962],{},"Treating a subslice as a copy."," Use ",[410,1965,1545],{}," or ",[410,1968,1969],{},"slices.Clone"," when you need independence.",[1932,1972,1973,1976],{},[778,1974,1975],{},"Keeping a tiny subslice of a huge slice."," The whole underlying array stays in memory. Copy the part you need.",[1932,1978,1979,1988],{},[778,1980,1981,1982,1984,1985,1987],{},"Modifying ",[410,1983,1734],{}," in a ",[410,1986,1632],{}," loop"," and expecting the slice to change.",[390,1990,1992],{"id":1991},"practice","Practice",[395,1994,1995,1996,1999,2000,475],{},"Write a function ",[410,1997,1998],{},"evens(numbers []int) []int"," that returns a new slice containing only the even values from the input. The input slice must not be modified. Test it with ",[410,2001,2002],{},"[]int{1, 2, 3, 4, 5, 6}",[2004,2005,2006],"accordion",{},[2007,2008,2011,2305],"accordion-item",{"icon":2009,"label":2010},"i-lucide-lightbulb","Show solution",[403,2012,2014],{"className":405,"code":2013,"language":407,"meta":408,"style":408},"package main\n\nimport \"fmt\"\n\nfunc evens(numbers []int) []int {\n    result := make([]int, 0, len(numbers))\n    for _, n := range numbers {\n        if n%2 == 0 {\n            result = append(result, n)\n        }\n    }\n    return result\n}\n\nfunc main() {\n    input := []int{1, 2, 3, 4, 5, 6}\n    output := evens(input)\n\n    fmt.Println(input)  \u002F\u002F [1 2 3 4 5 6]\n    fmt.Println(output) \u002F\u002F [2 4 6]\n}\n",[410,2015,2016,2025,2029,2042,2046,2071,2098,2118,2138,2159,2165,2171,2180,2185,2190,2203,2241,2258,2263,2281,2300],{"__ignoreMap":408},[413,2017,2018,2021],{"class":415,"line":416},[413,2019,2020],{"class":1019},"package",[413,2022,2024],{"class":2023},"snwKR"," main\n",[413,2026,2027],{"class":415,"line":462},[413,2028,466],{"emptyLinePlaceholder":465},[413,2030,2031,2034,2036,2039],{"class":415,"line":469},[413,2032,2033],{"class":1033},"import",[413,2035,690],{"class":678},[413,2037,472],{"class":2038},"sdLNw",[413,2040,2041],{"class":678},"\"\n",[413,2043,2044],{"class":415,"line":500},[413,2045,466],{"emptyLinePlaceholder":465},[413,2047,2048,2051,2054,2056,2059,2061,2063,2065,2067,2069],{"class":415,"line":524},[413,2049,2050],{"class":1019},"func",[413,2052,2053],{"class":478}," evens",[413,2055,482],{"class":427},[413,2057,1147],{"class":2058},"syre3",[413,2060,670],{"class":427},[413,2062,439],{"class":438},[413,2064,642],{"class":427},[413,2066,670],{"class":427},[413,2068,439],{"class":438},[413,2070,1062],{"class":427},[413,2072,2073,2076,2078,2080,2082,2084,2086,2088,2090,2092,2094,2096],{"class":415,"line":529},[413,2074,2075],{"class":419},"    result",[413,2077,424],{"class":423},[413,2079,832],{"class":478},[413,2081,835],{"class":427},[413,2083,439],{"class":438},[413,2085,448],{"class":427},[413,2087,864],{"class":431},[413,2089,448],{"class":427},[413,2091,1534],{"class":478},[413,2093,482],{"class":427},[413,2095,1147],{"class":419},[413,2097,1112],{"class":427},[413,2099,2100,2103,2105,2107,2110,2112,2114,2116],{"class":415,"line":1594},[413,2101,2102],{"class":1033},"    for",[413,2104,1693],{"class":419},[413,2106,448],{"class":427},[413,2108,2109],{"class":419}," n",[413,2111,424],{"class":423},[413,2113,1654],{"class":1033},[413,2115,1322],{"class":419},[413,2117,1062],{"class":427},[413,2119,2121,2124,2126,2129,2131,2134,2136],{"class":415,"line":2120},8,[413,2122,2123],{"class":1033},"        if",[413,2125,2109],{"class":419},[413,2127,2128],{"class":1019},"%",[413,2130,1228],{"class":431},[413,2132,2133],{"class":1049}," ==",[413,2135,864],{"class":431},[413,2137,1062],{"class":427},[413,2139,2141,2144,2146,2148,2150,2153,2155,2157],{"class":415,"line":2140},9,[413,2142,2143],{"class":419},"            result",[413,2145,541],{"class":423},[413,2147,715],{"class":478},[413,2149,482],{"class":427},[413,2151,2152],{"class":419},"result",[413,2154,448],{"class":427},[413,2156,2109],{"class":419},[413,2158,731],{"class":427},[413,2160,2162],{"class":415,"line":2161},10,[413,2163,2164],{"class":427},"        }\n",[413,2166,2168],{"class":415,"line":2167},11,[413,2169,2170],{"class":427},"    }\n",[413,2172,2174,2177],{"class":415,"line":2173},12,[413,2175,2176],{"class":1033},"    return",[413,2178,2179],{"class":419}," result\n",[413,2181,2183],{"class":415,"line":2182},13,[413,2184,459],{"class":427},[413,2186,2188],{"class":415,"line":2187},14,[413,2189,466],{"emptyLinePlaceholder":465},[413,2191,2193,2195,2198,2201],{"class":415,"line":2192},15,[413,2194,2050],{"class":1019},[413,2196,2197],{"class":478}," main",[413,2199,2200],{"class":427},"()",[413,2202,1062],{"class":427},[413,2204,2206,2209,2211,2213,2215,2217,2219,2221,2223,2225,2227,2229,2231,2233,2235,2237,2239],{"class":415,"line":2205},16,[413,2207,2208],{"class":419},"    input",[413,2210,424],{"class":423},[413,2212,670],{"class":427},[413,2214,439],{"class":438},[413,2216,442],{"class":427},[413,2218,536],{"class":431},[413,2220,448],{"class":427},[413,2222,590],{"class":431},[413,2224,448],{"class":427},[413,2226,595],{"class":431},[413,2228,448],{"class":427},[413,2230,931],{"class":431},[413,2232,448],{"class":427},[413,2234,950],{"class":431},[413,2236,448],{"class":427},[413,2238,955],{"class":431},[413,2240,459],{"class":427},[413,2242,2244,2247,2249,2251,2253,2256],{"class":415,"line":2243},17,[413,2245,2246],{"class":419},"    output",[413,2248,424],{"class":423},[413,2250,2053],{"class":478},[413,2252,482],{"class":427},[413,2254,2255],{"class":419},"input",[413,2257,731],{"class":427},[413,2259,2261],{"class":415,"line":2260},18,[413,2262,466],{"emptyLinePlaceholder":465},[413,2264,2266,2268,2270,2272,2274,2276,2278],{"class":415,"line":2265},19,[413,2267,1087],{"class":419},[413,2269,475],{"class":427},[413,2271,479],{"class":478},[413,2273,482],{"class":427},[413,2275,2255],{"class":419},[413,2277,642],{"class":427},[413,2279,2280],{"class":496},"  \u002F\u002F [1 2 3 4 5 6]\n",[413,2282,2284,2286,2288,2290,2292,2295,2297],{"class":415,"line":2283},20,[413,2285,1087],{"class":419},[413,2287,475],{"class":427},[413,2289,479],{"class":478},[413,2291,482],{"class":427},[413,2293,2294],{"class":419},"output",[413,2296,642],{"class":427},[413,2298,2299],{"class":496}," \u002F\u002F [2 4 6]\n",[413,2301,2303],{"class":415,"line":2302},21,[413,2304,459],{"class":427},[395,2306,2307,2308,2311,2312,2314,2315,2318],{},"The function builds a brand-new slice, so the input is untouched. ",[410,2309,2310],{},"make([]int, 0, len(numbers))"," starts with length 0 and enough capacity for the worst case, so ",[410,2313,884],{}," never has to reallocate. If you wrote ",[410,2316,2317],{},"return numbers[:3]"," instead, the result would share storage with the input, which is exactly the trap this lesson warns about.",[390,2320,2322],{"id":2321},"summary","Summary",[395,2324,2325,2326,2328,2329,1966,2331,2333,2334,2336,2337,2339],{},"An array has a fixed length that is part of its type, and it is copied when assigned. A slice is a flexible window onto an underlying array, with a length and a capacity. Use ",[410,2327,884],{}," and always keep its result, and remember that slicing a slice shares the same storage, so use ",[410,2330,1545],{},[410,2332,1969],{}," when you need independent data. ",[410,2335,1632],{}," walks over slices, and ",[410,2338,1734],{}," in the loop is a copy.",[395,2341,2342,2343,2349,2350,475],{},"Official reference: ",[571,2344,2348],{"href":2345,"rel":2346},"https:\u002F\u002Fgo.dev\u002Fblog\u002Fslices-intro",[2347],"nofollow","Go Slices: usage and internals"," and the ",[571,2351,2354],{"href":2352,"rel":2353},"https:\u002F\u002Fgo.dev\u002Fref\u002Fspec#Array_types",[2347],"Array types section of the specification",[2356,2357],"learning-lesson-complete",{},[2359,2360],"learning-lesson-navigation",{"next":111,"nextTitle":110,"previous":101,"previousTitle":100},[2362,2363,2364],"style",{},"html pre.shiki code .s45-6, html code.shiki .s45-6{--shiki-light:#268BD2;--shiki-default:#EEFFFF;--shiki-dark:#E06C75}html pre.shiki code .su2KV, html code.shiki .su2KV{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#E5C07B}html pre.shiki code .shFME, html code.shiki .shFME{--shiki-light:#657B83;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .sAJzr, html code.shiki .sAJzr{--shiki-light:#D33682;--shiki-default:#F78C6C;--shiki-dark:#D19A66}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 .sisdC, html code.shiki .sisdC{--shiki-light:#268BD2;--shiki-default:#82AAFF;--shiki-dark:#61AFEF}html pre.shiki code .sJsHy, html code.shiki .sJsHy{--shiki-light:#93A1A1;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#7F848E;--shiki-dark-font-style:italic}html .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 .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 .sN42N, html code.shiki .sN42N{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#ABB2BF}html pre.shiki code .sEOdo, html code.shiki .sEOdo{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#C678DD}html pre.shiki code .s7Bcg, html code.shiki .s7Bcg{--shiki-light:#859900;--shiki-light-font-style:inherit;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#C678DD;--shiki-dark-font-style:inherit}html pre.shiki code .sRBoR, html code.shiki .sRBoR{--shiki-light:#859900;--shiki-default:#89DDFF;--shiki-dark:#56B6C2}html pre.shiki code .snwKR, html code.shiki .snwKR{--shiki-light:#CB4B16;--shiki-default:#FFCB6B;--shiki-dark:#E5C07B}html pre.shiki code .sdLNw, html code.shiki .sdLNw{--shiki-light:#2AA198;--shiki-default:#FFCB6B;--shiki-dark:#98C379}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}",{"title":408,"searchDepth":462,"depth":462,"links":2366},[2367,2368,2369,2370,2371,2373,2374,2375,2376,2378,2379,2380,2381,2382],{"id":392,"depth":462,"text":393},{"id":400,"depth":462,"text":401},{"id":651,"depth":462,"text":652},{"id":799,"depth":462,"text":800},{"id":880,"depth":462,"text":2372},"Growing with append",{"id":1122,"depth":462,"text":1123},{"id":1267,"depth":462,"text":1268},{"id":1485,"depth":462,"text":1486},{"id":1628,"depth":462,"text":2377},"Looping with range",{"id":1741,"depth":462,"text":1742},{"id":1867,"depth":462,"text":1868},{"id":1926,"depth":462,"text":1927},{"id":1991,"depth":462,"text":1992},{"id":2321,"depth":462,"text":2322},"Store ordered values with arrays and slices, and understand length, capacity, append, copy, and shared backing arrays.","md",null,{},{"icon":108},{"description":2389,"title":105},"Learn Go arrays and slices with clear explanations of append, range, length, capacity, slicing, copying, nil slices, and the shared backing array pitfall.","F4AU3Pg7VgvgrWjT_uBebOkNXSmEhL2g9-Ec6qoX3mI",[2392,2394],{"title":100,"path":101,"stem":102,"description":2393,"icon":103,"children":-1},"Understand copies, addresses, dereferencing, nil pointers, and when a pointer is the right choice.",{"title":110,"path":111,"stem":112,"description":2395,"icon":113,"children":-1},"Store key-value data, and safely read, update, delete, and iterate over maps.",1794118099334]