{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreieyplh57howjukrbivahsdyki3to3qegyrvo4eq5hojgrzbokhj4q",
"uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mozn6i6gswn2"
},
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreib4zdupwj362fxu73n4w3ikkgxktxkl5simt72krptxahpayhea74"
},
"mimeType": "image/webp",
"size": 29768
},
"path": "/frihk_ian/array-in-go-mad",
"publishedAt": "2026-06-24T09:33:49.000Z",
"site": "https://dev.to",
"tags": [
"go",
"beginners"
],
"textContent": "## What is an Array in Go?\n\nIn Go, an array is a fixed-size, ordered set of values with the same type. Imagine an array as a 12 egg tray; the minimum number of eggs which can fit into an array is 12; the maximum number of eggs which can fit into an array is 12. That is the exact behaviour of a Go array.\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n\n var groceries [3]string// Step 1: Create an array that holds 3 strings\n groceries[0] = \"Bread\"// Step 2: Put \"Bread\" in slot 0\n groceries[1] = \"Milk\"// Step 3: Put \"Milk\" in slot 1\n groceries[2] = \"Eggs\"// Step 4: Put \"Eggs\" in slot 2\n fmt.Println(groceries)// Step 5: Print the whole array\n }\n\n\nExpected output:\n`[Bread Milk Eggs]`\n\n## Zero Values\n\nIn Go, you create an array by using the var keyword, and each of the slots receives the safe code Zero value, given that no elements are explicitly set.\n\nThis means that your array will never be in an unsafe state of being uninitialized. It will be valid on every slot, even before set. The same thing is a safety feature in Go intentionally.\n\n\n\n var prices [3]float64\n\n fmt.Println(prices) // Output: [0 0 0] - Go fills all the values with zeros automatically\n\n\n## Declaring an Array\n\nIn Go, you can declare an array in a few different ways, and you will learn about each of them in turn.\n\n#### Using the var Declaration\n\n\n var groceries [3]string\n groceries[0] = \"Bread\"\n groceries[1] = \"Milk\"\n groceries[2] = \"Eggs\"\n\n\nIf you want to initialise an array of a specific size and use it later, then this is the best method to achieve this.\n\n#### Array Literals\n\nIf you are already aware of all the values at the time you write the code, then you can use the literal syntax:\n\n\n\n package main\n\n import \"fmt\"\n\n func main() {\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n fmt.Println(groceries)\n }\n\n\nExpected output:\n`[Bread Milk Eggs]`\n\nThe actual syntax is:\n`[size]type{value1, value2, value3}`\nThis method is used when we know the values which we want to store in an array.\n\n#### Let the Compiler count for you — The `...` trick\n\nWhen using literal syntax, and you find that you have to manually count yourself, there's a shortcut in Go. Use ... to fill the size so the compiler can calculate the length automatically.\n\n\n\n groceries := [...]string{\"Bread\", \"Milk\", \"Eggs\", \"Butter\", \"Coffee\"} //Three dots compiler counts for you\n\n\nThe type of groceries is [5]string. The ... instructs the compiler to count for you. This will be helpful if you have a super long list and you can't determine its length.\n\n#### Sparse Initialization\n\nYou can also use index: value syntax in the braces: to specify where a value will go:\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n // Put \"Bread\" in slot 0 and \"Coffee\" in slot 4; all other slots get \"\"\n groceries := [5]string{0: \"Bread\", 4: \"Coffee\"}\n fmt.Println(groceries)\n }\n\n\n\nExpected output:\n`[Bread Coffee]`\n\nThis is helpful when you have an array of a large number of elements, but just a couple of elements to change.\n\n## How to access elements and modify an array.\n\nEach element of an array is retrieved by the array name and an index number enclosed in square brackets.\n\n\n\n package main\n\n import \"fmt\"\n\n func main() {\n\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n\n fmt.For each element, read it on the screen:\n Println(groceries[0])// Read \"Bread\" on the screen\n fmt.Println(groceries[2])// Read slot 2: Eggs\n groceries[0] = \"Brown Bread\" // Change the value of the 0th element of the array.\n fmt.Println(groceries[0])// Read slot 0 again → \"Brown Bread\"\n }\n\n\nExpected output:\n\n\n\n Bread\n Eggs\n Brown Bread\n\n\n#### What are the consequences of going beyond the lines?\n\nIf you try to access a non-existent part of a structure (for example, groceries[5] for a 3-element array), then Go will immediately exit the program with a runtime panic:\nruntime error: index out of range [5] with length 3\nNot a silent bug this time. Go lets you know what was amiss. len(array) - 1 is the final valid index in the array.\n\n## Getting the Length with len()\n\nlen() returns the size (number of slots) of an array:\n\n\n\n package main\n\n import \"fmt\"\n\n func main() {\n groceries := [5]string{\"Bread\", \"Milk\", \"Eggs\", \"Butter\", \"Coffee\"}\n fmt.Println(\"Items on list:\", len(groceries))\n }\n\n\nExpected output:\n`Items on list: 5`\n\nIf an array is declared with a size, then len() always returns the exact size, irrespective of whether there are values stored in some of the array's slots. An array is always the size that it was created to be.\n\nAlso, there is an operation cap(), which is called \"capacity\". Arrays cannot be lengthened or shortened, but the two functions, cap() and len(), return the same value. Use len(arr) instead of literals for the size. If you ever change the array declaration, len() adjusts automatically.\n\nWhen the array is of length, len() always returns the size (independent of whether or not there are zeroes in the array). An array is always allocated the memory space it was declared to have.\n\n## Arrays are valued, not references.\n\nAs a result of Go's copy behavior on assigning an array to a new variable, the entire array is copied. There are two independent variables – if one changes, the other one definitely will not change at all. Let's say we think of an example;\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n backup := groceries// This copies ALL of groceries into backup\n \n groceries[0] = \"Brown Bread\"// Change the original\n \n fmt.Println(\"groceries:\", groceries)\n fmt.Println(\"backup:\", backup)\n }\n\n\n\nExpected output:\n\n\n\n groceries: [Brown Bread Milk Eggs]\n backup:[Bread Milk Eggs]\n\n\n\nThe above shows that despite replacing groceries [0] with ‘Brown Bread’, backup was not affected. This is similar to photocopying your shopping list! After they've made the photocopy, it remains unchanged if they write on the original. They started identical, but are now two completely separate pieces of paper.\n\n## Sharing With Pointers\n\nIf we want, the changes that we make with our new variable to affect the original array, it's using a pointer.\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n shared := &groceries// & means \"give me the address of groceries\"\n \n groceries[0] = \"Brown Bread\"\n \n fmt.Println(\"groceries:\", groceries)\n fmt.Println(\"shared:\", *shared)// * means \"follow the pointer to the value\"\n }\n\n\n\nExpected output:\n\n\n\n groceries: [Brown Bread Milk Eggs]\n shared:[Brown Bread Milk Eggs]\n\n\n&groceries make a pointer. You don't copy the array; you copy a small \"address\" indicating where the array is stored in memory. That is followed by the word ‘*shared follows that address to get the actual value. When Shared access is needed, Pointers are used. In most situations, you'll opt for the default copy behaviour, which is more secure and easier to do.\n\n## Comparing Arrays\n\nGo and share how you can use “==” and “!=” to compare two arrays of equal “size” and “type”.\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n list1 := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n list2 := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n list3 := [3]string{\"Bread\", \"Milk\", \"Coffee\"}\n \n fmt.Println(\"Same?\", list1 == list2)// true\n fmt.Println(\"Same?\", list1 == list3)// false\n }\n\n\n\nExpected output:\n\n\n\n Same? true\n Same? false\n\n\n\nTwo arrays are equal if they are of the same type and length and the elements of the arrays in the corresponding positions are all equal. You must have both conditions.\nComparison of differently sized arrays is not possible. The compiler will halt you, since they are of different types.\n\nLooping Through Arrays\nFor small arrays, manually writing the below would work: `groceries[0], groceries[1], groceries[2]`. Anything larger, you just use a loop to visit each element by itself. Some of the loops you can use to accomplish this are listed here:\n\n#### The for range Loop\n\nThe most popular looping over an array in Go is the following for range loop:\n\n\n\n package main\n import \"fmt\"\n func main() {\n groceries := [5]string{\"Bread\", \"Milk\", \"Eggs\", \"Butter\", \"Coffee\"}\n For each element (index, item) in the groceries: {\n fmt.println(\"Index: \" + index + \" Item: \" + item)\n }\n }\n\n\nExpected output:\n\n\n\n Index: 0 Item: Bread\n Index: 1 Item: Milk\n Index: 2 Item: Eggs\n Index: 3 Item: Butter\n Index: 4 Item: Coffee\n\n\nThe index will contain the number of the slot that is being modified on each iteration through the loop, and the item will contain a copy of the element being modified. The loop stops by itself when it runs out of elements. No more worrying about out of bounds.\n\nIn Go programming, all the variables defined must be used. The for-range loop provides Go with two variables in its \"for\" clause: the index and element variable. If the index isn’t useful to you, then you can't just leave it there intact. It will refuse to compile with Go.\n\nThis is where the blank identifier `_` is introduced. It is Go's way of letting you say, \"I know this value exists, but I am deliberately throwing it away.\"\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n groceries := [5]string{\"Bread\", \"Milk\", \"Eggs\", \"Butter\", \"Coffee\"}\n \n for _, item := range groceries {\n fmt.Println(\"Item:\", item)\n }\n }\n\n\nExpected output:\n\n\n\n Item: Bread\n Item: Milk\n Item: Eggs\n Item: Butter\n Item: Coffee\n\n\nIt's also possible to discard the value and keep the index. If so, simply do not use the second variable at all:\n\n\n\n For each index in the groceries array {\n fmt.Println(\"Index:\", index)\n }\n\n\n#### A classic for Loop.\n\nIn the case where you require more control, such as when going in reverse or skipping an element, the classic counter-based for loop is best:\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n groceries := [5]string{\"Bread\", \"Milk\", \"Eggs\", \"Butter\", \"Coffee\"}\n \n for i := 0; i < len(groceries); i++ {\n fmt.Println(\"Position\", i, \"->\", groceries[i])\n }\n }\n\n\n\nExpected output:\n\n\n\n Position 0 -> Bread\n Position 1 -> Milk\n Position 2 -> Eggs\n Position 3 -> Butter\n Position 4 -> Coffee\n\n\n\nIterating in reverse:\n\n\n\n for i := len(groceries) - 1; i >= 0; i-- {\n fmt.Println(groceries[i])\n }\n\n //`Output: Coffee → Butter → Eggs → Milk → Bread`\n\n\nPrinting every other item:\n\n\n\n for i := 0; i < len(groceries); i += 2 {\n fmt.Println(groceries[i])\n }\n\n //`Output: Bread → Eggs → Coffee (slots 0, 2, and 4)`\n\n\n## Multi-Dimensional Arrays\n\nSo far, all the arrays have been a single row of values (one-dimensional). Multi-dimensional arrays (arrays of arrays) are also supported in Go.\nThe most common case is a 2D array, which is a grid or a table. For instance, if I go to the shops every week and make a plan for the items I want to buy by each day and area of the shop:\n\n\n\n package main\n \n import \"fmt\"\n \n func main() {\n // A 3-day plan with 2 items per day\n weeklyPlan := [3][2]string{\n {\"Bread\", \"Milk\"},// Day 0\n {\"Eggs\", \"Butter\"},// Day 1\n {\"Coffee\", \"Fruit\"},// Day 2\n }\n \n for day, items := range weeklyPlan {\n fmt.Printf(\"Day %d: %s and %s\\n\", day, items[0], items[1])\n }\n }\n\n Day 0: Bread and Milk\n Day 1: Eggs and Butter\n Day 2: Coffee and Fruit\n\n\n`[3][2]string`: An array of 3 elements, each of which is an array of 2 strings. The first index tells you which row to choose, the second index tells you which column to choose:\n\n\n\n weeklyPlan[0][0] = \"Bread\"// Day 0, first item\n weeklyPlan[0][1] = \"Milk\"// Day 0, second item\n weeklyPlan[2][1] = \"Fruit\"// Day 2, second item\n\n\nAn index position is needed to reference a particular cell, the first being the row index, and the second, the column index.\npassing an Array object to a function.\nIn Go, an array passed to a function is itself a complete copy of the array; the function does not refer to the original array. Any changes the function makes don't affect the original:\n\n\n\n package main\n \n import \"fmt\"\n \n func printAndModify(list [3]string) {\n list[0] = \"Changed\"// Modifies the LOCAL copy only\n fmt.Println(\"Inside:\", list)\n }\n \n func main() {\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n printAndModify(groceries)\n fmt.Println(\"Outside:\", groceries)// Original is unchanged\n }\n\n\n\nExpected output:\n\n\n\n Inside: [Changed Milk Eggs]\n Outside: [Bread Milk Eggs]\n\n\nThe function takes a snapshot of an array that is independent of one another.\nIf you have to change the original function, provide a pointer:\n\n\n\n package main\n \n import \"fmt\"\n \n func updateFirstItem(list *[3]string) {\n list[0] = \"Brown Bread\"// This modifies the original\n }\n \n func main() {\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n updateFirstItem(&groceries)\n fmt.Println(\"Outside:\", groceries)// Now the original IS changed\n }\n\n\n\nExpected output:\n`Outside: [Brown Bread Milk Eggs]`\n\nIf you have a large array greater than a few hundred elements, always use a pointer instead of copying to avoid the cost of the copy.\n\n## Limitations of Arrays\n\nThough concise and straightforward, arrays are restricted in their use and come with limitations that you'll hit hard soon.\n\n#### The Size Is Fixed Forever\n\nAfter defining a [3]string, it will always contain exactly 3 strings, always! You can't add a fourth item. You can't remove one to shrink it. The size is baked in at compile time. If your shopping list grows to 6 items, your [5]string array can't help you.\nThe array size can not be a variable, must be a literal number or named constant:\n\n\n\n n := 5\n var groceries [n]string\n\n\nThe above is a compiler error because of the occurrence of the variable n.\n\n\n\n Fix:\n const n = 5\n var groceries [n]string// it Works, n is a constant\n\n\nThis means you can't create an array whose size depends on something you only know at runtime, like how many items the user typed in. For that, you need a slice.\n\n#### Passing Large Arrays Is Expensive\n\nEvery function call copies the entire array. For a [10000]string, that is 10,000 strings copied just to call a function.\nArrays of Different Sizes Are Incompatible Types\nA function that accepts [3] strings can't be called with a [5]string. You would need two separate functions with different signatures.\n`append()` Does Not Work on Arrays\n`append()` — the function that adds elements to a collection — only works on slices, not arrays:\n\n\n\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n groceries = append(groceries, \"Butter\")// Compiler `error`: append expects a slice\n\n\n\nThese limitations are not bugs. Fixed-size value types are simple, predictable, and give the compiler strong guarantees. But most real-world programming needs something that can grow and shrink at runtime. That something is the slice.\n\n## Common Mistakes with Arrays\n\n#### Mistake 1: Assigning Arrays of Different Sizes\n\n\n list := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n var biggerList [4]string = list// Compiler `error` — different types\n\n\n\nFix: make sure both arrays have the same size, or use a slice if you need flexibility.\n\n\n#### Mistake 2: Expecting Assignment to Share Data\n\n\n list := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n backup := list\n backup[0] = \"Brown Bread\"\n fmt.Println(list[0])// Prints \"Bread\", NOT \"Brown Bread\" — list was not changed\n\n\nArrays are value types. Assignment copies everything. If you want shared access, use &list to get a pointer, or switch to slices.\n\nMistake 3: Off-by-One on the Last Element\n\n\n\n groceries := [3]string{\"Bread\", \"Milk\", \"Eggs\"}\n fmt.Println(groceries[3])// Runtime `panic`: index out of range\n\n\nFor an array of length 3, the valid indexes are 0, 1, and 2. The last valid index is always len(array) - 1.",
"title": "Array in Go"
}