Start learning free

LESSON 101 OF 671 · ARRAYS I

Static Arrays in Go

Lesson

When we're dealing with large amounts of data, we want to make sure we can organize and manage it properly.

In Go, we use arrays to store several pieces of data in one place. The items in an array must be of the same data type.

A static array or fixed array is an array for which the size / length is determined when the array is created and/or allocated.


Creation:

We create a static array by writing the var keyword, the array's name, the array's length inside [], and the data type of its elements:

var arrayOfStrings [5]string

In this program we created an array named arrayOfStrings that consists of 5 string items.

These 5 items are initialized with empty strings "" automatically.


Creation & Initialization:

There is a way to initialize an array at creation by using the := operator, the length marker [...] (the compiler counts the values), the element type, and the values inside curly braces {} with a comma between each entry, like this:

arrayOfStrings := [...]string{"AlgoCademy", "coding", "static arrays"}

We created an array named arrayOfStrings which consists of 3 string items: "AlgoCademy", "coding" and "static arrays".


Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check