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

SlicesSlices

Slices are akin to arrays, but they possess dynamic lengths, making them a more flexible alternative to arrays.

To create a slice, we utilize the same syntax as for arrays, but in this case, we omit specifying the size.

Creating a slice using the var keyword:

Create a Slice using the := operator:

We can add additional elements to a slice using the append function. The append function returns a new slice with the appended elements, which we can then store in the original slice variable. This will become clearer with an example. The basic syntax of the append function is as follows:

The following code demonstrates the use of the append function:

go

index.go

In the same way that we can access elements within an array, we can also reference portions of an array using the following syntax:

go

index.go

It will extract all elements from the startingIndex to the endingIndex. Please note that we need to provide endingIndex + 1. This will become clearer with an example. For example:

go

index.go

It extracts the elements from indexes 4 to 9, including the element at index 4 and excluding the element at index 9, as expressed in the syntax:

go

index.go

We can also store this extracted portion of the array in a variable, thus creating a slice:

go

index.go

It's important to note that such a slice references the portion of the array from which it was created. Consequently, any changes made in that slice will also affect the original array:

go

index.go

What will be the output of the following code?

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

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

Секція 5. Розділ 3
course content

Зміст курсу

Introduction to GoLang

SlicesSlices

Slices are akin to arrays, but they possess dynamic lengths, making them a more flexible alternative to arrays.

To create a slice, we utilize the same syntax as for arrays, but in this case, we omit specifying the size.

Creating a slice using the var keyword:

Create a Slice using the := operator:

We can add additional elements to a slice using the append function. The append function returns a new slice with the appended elements, which we can then store in the original slice variable. This will become clearer with an example. The basic syntax of the append function is as follows:

The following code demonstrates the use of the append function:

go

index.go

In the same way that we can access elements within an array, we can also reference portions of an array using the following syntax:

go

index.go

It will extract all elements from the startingIndex to the endingIndex. Please note that we need to provide endingIndex + 1. This will become clearer with an example. For example:

go

index.go

It extracts the elements from indexes 4 to 9, including the element at index 4 and excluding the element at index 9, as expressed in the syntax:

go

index.go

We can also store this extracted portion of the array in a variable, thus creating a slice:

go

index.go

It's important to note that such a slice references the portion of the array from which it was created. Consequently, any changes made in that slice will also affect the original array:

go

index.go

What will be the output of the following code?

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

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

Секція 5. Розділ 3
some-alt