- How to iterate over an Array using for loop?
- Part 11: Arrays and Slices
- Working with Arrays in Golang
- Golang Arrays
- Golang Arrays
How to iterate over an Array using for loop?Welcome to the part 11 of Golang tutorial series. In this tutorial we deal with Arrays and Slices in Go. An array is a collection of elements that belong to the same type. For example the collection of integers 5, 8, 9, 79, 76 form an array. Mixing values of different types, for example an array that contains both strings and integers is not allowed in Go. An array belongs to type [n]T. The number of elements n is also a part of the type We will discuss this in more detail shortly. All elements in an array are automatically assigned the zero value of the array type. In this case a is an integer array and hence all elements of a are assigned to 0the zero value of int. Running the above program will output [0 0 0]. The index of an array starts from 0 and ends at length - 1. Lets assign some values to the above array. The program will output [12 78 50]. It is not necessary that all elements in an array have to be assigned a value during short hand declaration. In the above program in line no. The remaining 2 elements are assigned 0 automatically. The program will output [12 0 0]. You can even ignore the length of the array in the declaration and replace it with This is done in the following program. The size of the array is a part of the type. Hence int and int are distinct types. Because of this, arrays cannot be resized. Don't worry about this restriction since slices exist to overcome this. In line no. Arrays in Go are value types and not reference types.
Part 11: Arrays and Slices
Working with Arrays in Golang
An array is a fixed-size collection of elements of the same type. The elements of the array are stored sequentially and can be accessed using their index. By default, all the array elements are initialized with the zero value of the corresponding array type. For example, if we declare an integer array, all the elements will be initialized with 0. If we declare a string array, all the elements will be initialized with an empty string ""and so on. The elements of an array are stored sequentially and can be accessed by their index. The index starts from zero and ends at length - 1. Note that we do not need to specify the type of the variable a as in var a intbecause the compiler can automatically infer the type from the expression on the right hand side. The above array declaration can also be written as below inside any function. You can also omit the size declaration from the initialization expression of the array, and let the compiler count the number of elements for you. The length of an array is part of its type. So the array aint and aint are completely distinct types, and you cannot assign one to the other. This also means that you cannot resize an array, because resizing an array would mean changing its type, and you cannot change the type of a variable in Golang. This means that when you assign an array to a new variable or pass an array to a function, the entire array is copied. In the example below, we find the sum of all the elements of the array by iterating over the array, and adding the elements one by one to the variable sum. Golang provides a more powerful form of for loop using the range operator. Here is how you can use the range operator with for loop to iterate over an array. The above program now runs successfully and outputs the sum of the array. All the arrays that we created so far in this post are one dimensional. You can also create multi-dimensional arrays in Golang. Arrays are useful but a bit inflexible due to the limitation caused by their fixed size. And this is why Go provides another data structure called Slice that builds on top of arrays and provides all the flexibility that we need. Next Article: Introduction to Slices in Golang. Code Samples: github. Liked the Article? Share it on Social media!