Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Accessing and Modifying Array Elements | Section
Practice
Projects
Quizzes & Challenges
Quizze
Challenges
/
Getting Started with Go

bookAccessing and Modifying Array Elements

Swipe um das Menü anzuzeigen

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:

arrayName[elementIndex]

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:

index.go

index.go

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:

index.go

index.go

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]
question mark

What will be the output of the following code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 35

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 35
some-alt