Search⌘ K

Array Literals and Parameters

Explore how to initialize arrays using literals in Go, including explicit sizes and index syntax. Understand how to pass arrays to functions efficiently by using pointers or slices to avoid memory overhead.

Array literals

When the values (or some of them) of the items are known beforehand, a simpler initialization exists using the { , ,} notation called array literals (or constructors) instead of initializing every item in the [ ]= way. Let’s see some variants.

1st variant

Specify explicitly the size n of the array with [n]. For example:

var arrAge = [5]int{18, 20, 15, 22, 16}

Here is another example:

 ...