Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing and Modifying Array Elements | Arrays and Slices
Introduction to GoLang
course content

Зміст курсу

Introduction to GoLang

Introduction to GoLang

1. Getting Started
2. Data Types
3. Control Structures
4. Functions
5. Arrays and Slices
6. Intro to Structs & Maps

bookAccessing and Modifying Array Elements

In the previous chapter, we learned how to declare and initialize an array. In this chapter, we will explore how to access and modify individual elements of an array.

Each element in the array is assigned an index that represents its position within the array. The first element of an array is assigned the index 0, the second element has the index 1, and so forth. It's important to note that when an array contains ten elements, the last element will have an index of 9, as indexing starts from 0.

Here is the syntax for referencing an element within an array:

Note

The process of accessing array elements by their indexes is referred to as indexing.

Let's revisit the students array we discussed in the previous chapter. We can access its second element using indexing:

go

index

copy
12
var students = [4] string { "Luna", "Max", "Ava", "Oliver" } fmt.Println(students[1]) // Output: Max

An array functions as a collection of variables. When we reference an element using indexing, we are essentially accessing a variable. Consequently, we can also modify it:

go

index

copy
1234
var students = [4] string { "Luna", "Max", "Ava", "Oliver" } fmt.Println(students) // Output: [Luna Max Ava Oliver] students[1] = "Tom" fmt.Println(students) // Output: [Luna Tom Ava Oliver]
What will be the output of the following code?

What will be the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt