Introduction

Welcome to our enlightening lesson about arrays in Go, a fundamental data structure. Think of arrays as a collection of lockers at a school, each storing a specific item. Arrays help us organize our data, facilitating easy retrieval and manipulation.

In this captivating journey, we'll unravel arrays in Go, focusing on their creation, element access, and unique properties.

What are Arrays?

An array is a container that holds a sequence of elements of the same type. Much like a bookshelf with specified slots, each housing a book, an array has numbered slots that store items (referred to as elements).

Creating Arrays in Go

Creating an array in Go is straightforward. The syntax is as follows:

We can declare and initialize an array simultaneously like this:

This statement declares the array hoursStudied and supplies it with the hours studied.

Accessing Array Elements in Go

Accessing the elements in an array is akin to fetching a specific book from a shelf. For example, to retrieve the first two days of study from our hoursStudied array, we would do the following:

Here, [0] fetches the first element and fetches the second. Be careful not to access an index that is out of range; doing so would trigger a runtime error in Go.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal