# 20. Arrays

### Array Type

Arrays are one of the fundamental type in any programming languages. Arrays are generally used to store *list of things* of similar type.

In Go, an array is a fixed-length sequence of zero or more elements of a particular type. That means, at run-time array can not grow or shrink. Due to this limitation, arrays are rarely used directly in Go

### Declaring array&#x20;

While declaring array, we need to specify size and type of array.

Below are some of array declarations:

{% code title="main.go" %}

```go
var numbers [8]float64
var days [5]string
var marks [10]int
```

{% endcode %}

1st line declares array named `numbers` which can hold up-to 8 `float64` values.

2nd declares array named `songs` which can hold up-to 5 `string` values&#x20;

### Accessing array elements

Since an array is collection of item, individual items of an array can be accessed by using square brackets `[]` with an index that begins at `0`

{% hint style="danger" %}
Remember array index starts at `0` and not `1`
{% endhint %}

{% code title="main.go" %}

```go
var days [7]string
// days[0] is the first element
days[0] = "Sunday"
// days[6] is the last element
days[6] = "Saturday"
```

{% endcode %}

If we try to access an element out or array index, Go compiler will give us error

{% code title="main.go" %}

```go
days[7] = "Funday"

```

{% endcode %}

`output: invalid array index 7 (out of bounds for 7-element array)`

### Zero Values

When a new array is created, all the values are initialized to the zero value for the type. Zero Values ensures that array is not uninitialized.

{% code title="main.go" %}

```go
var numbers [8]float64
var marks [10]int
fmt.Println(numbers, marks) 
// both numbers and marks elements get their zero values
//output : [0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]
```

{% endcode %}

### Array Literals

We can assign values to an array while declaring using Array Literals.

{% code title="main.go" %}

```go
var data [2]string = [2]string{
		"Hello",
		"World!",
	}
```

{% endcode %}

Short variable declaration can also be used for array declaration

{% code title="main.go" %}

```go
newData := [2]string{"Hello", "World"}
fmt.Println(newData)
// output [Hello World!]
```

{% endcode %}

### Ellipsis

&#x20;if an ellipsis “`...`” appears in place of the length, the array length is determined by the number of elements

{% code title="main.go" %}

```go
arr := [...]string{"Hello", "World", "From Go"}
fmt.Println(arr)
	
```

{% endcode %}

### Iterating over Array

We can use traditional index to iterate over an array.When accessing array elements using a index variable, we need to be careful. Trying to access an index that is outside the array will cause a run-time panic!

{% code title="main.go" %}

```go
days = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}

for index := 0; index < 7; index++ {
    fmt.Println(days[index])
}
```

{% endcode %}

A safer way is to use `len()` function to calculate length of array being iterated.

{% code title="main.go" %}

```go
for index := 0; index < len(days); index++ {
    fmt.Println(days[index])
}
```

{% endcode %}

A more idiomatic way is to use `for...range` to loop over an array. `range` returns each array index and item.&#x20;

{% code title="main.go" %}

```go
for i, day := range days {
		fmt.Println(i, day)
	}
```

{% endcode %}

If index is not required, it can be ignored using blank identifier

{% code title="main.go" %}

```go
for _, day := range days {
		fmt.Println(day)
	}
```

{% endcode %}

### Type Of Array

Type of array is determined by what type of data array stores as well as length of array. If two array's store same type of data but have different lengths, Go will treat those arrays as of two different types.

{% code title="main.go" %}

```go
greetings := [...]string{"Hello", "World"}
greetings = [...]string{"Hello", "World", "From Go"}
//output : cannot use [3]string literal (type [3]string) as type [2]string in assignment
```

{% endcode %}

### Comparing Arrays

Arrays of similar types can be compared using `==` and `!=` . Remember, for arrays to be to of similar types, they need to store same type of data as well as their length must be same.&#x20;

{% code title="main.go" %}

```go
a := [2]string{"Hello", "World"}
b := [...]string{"Hello", "World"}
fmt.Println(a == b)
```

{% endcode %}

### GitHub Code

{% embed url="<https://github.com/gophersumit/codewithgo-samples/blob/master/array/main.go>" %}

### Blog Posts

{% embed url="<https://blog.golang.org/slices>" %}
