Go arrays

Arrays are one of the most popular data structures for two reasons: arrays are simple and easy to understand and they are very versatile and can store many different kinds of data.

You can declare an array that stores four integers as follows:

anArray := [4]int{1, 2, 4, -4} 

The size of the array is stated before its type, which is defined before its elements. You can find the length of an array with the help of the len() function: len(anArray).

The index of the first element of any dimension of an array is 0, the index of the second element of any array dimension is 1, and so on. This means that for an array with one dimension named a, the valid indexes are from 0 to len(a)-1.

Although you might be familiar with accessing the elements of an array in other programming languages using a for loop and a numeric variable, there are cooler ways to visit all of the elements of an array in Go that involve the use of the range keyword and that allow you to bypass the use of the len() function in the for loop. Look to the Go code of loops.go for such an example.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.148.210