GOLANG
The anatomy of Arrays in Go
An array is a container that holds values of the same type. Arrays in Go have fixed length and once they are defined, they can’t be expanded to fit more data.
☛ What is an array
An array is a collection of the same data type. For example, an array of integers or an array of strings. Since Go is a statically typed language, mixing different values belonging to different data types in an array is not allowed.
In Go, an array has a fixed length. Once defined with a particular size, the size of an array cannot be increased or decreased. But that problem can be solved using slices which we will learn in the next lesson.
Array is a composite or abstract data type because it is composed of primitive or concrete data types like
int
,string
,bool
etc.
☛ How to declare an array?
An array is a type in itself. This type is defined like [n]T
where n
is the number of elements an array can hold and T
is a data type like int
or string
.
Hence to define a variable which is an array of 3
elements of the type int
, it has the following syntax.
package main