Andrew's Digital Garden

Arrays in Go

Arrays are a composite type. They contain a list of values of one particular type. They are declared with their maximum size, and the type they hold. var name [10]string

Arrays cannot be resized. Thus, slices are more commonly used. Fixed-size arrays have the advantage of more control of memory usage.

[[20200720115117-go-slices]]

Array literals can be used to declare a variable and sets its values:

var a [3]int = [3]int{8, 16, 32}

When using an array literal, the size is not strictly necessary:

a := [...]int{8, 16, 32}

Arrays are values, which means reassinging an array to another copies all the elements, and passing an array to a function will pass a copy, rather than a pointer.

[[go]]

Referred in

Arrays in Go