Array in Go
What is an Array in Go? In 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. package main import "fmt" func main() { var groceries [3]string// Step 1: Create an array that holds 3 strings groceries[0] = "Bread"// Step 2: Put "Bread" in slot 0 groceries[1] = "Milk"// Step 3: Put "Milk" in slot 1 groceries[2] = "Eggs"// Step 4: Put "Eggs" in slot 2 fmt.Println(groceries)// Step 5: Print the whole array } Expected output: [Bread Milk Eggs] Zero Values In 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. This 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. var prices [3]float64 fmt.Println(prices) // Output: [0 0 0] - Go fills all the values with zeros automatically Declaring an Array In Go, you can declare an array in a few different ways, and you will learn about each of them in turn. Using the var Declaration var groceries [3]string groceries[0] = "Bread" groceries[1] = "Milk" groceries[2] = "Eggs" If you want to initialise an array of a specific size and use it later, then this is the best method to achieve this. Array Literals If you are already aware of all the values at the time you write the code, then you can use the literal syntax: package main import "fmt" func main() { groceries := [3]string{"Bread", "Milk", "Eggs"} fmt.Println(groceries) } Expected output: [Bread Milk Eggs] The actual syntax is: [size]type{value1, value2, value3} This method is used when we know the values which we want to store in an array. Let the Compiler count for you — The ... trick When using literal syntax, and you find that you