Slices in Golang

Tram Ho


Slices is a data structure also gives you a way to work and manage collections (collections) data. Slices are built on the idea of ​​an array that can be flexible in terms of the number of elements. Slices can be flexible when adding elements simply because it has built in append functionality, it can help me add elements to slices quickly and efficiently. You can also reduce the size of slices by taking a portion of the memory below. Slices also give you all the convenience of indexing, iteration, and garbage collection optimizations because the underlying memory is arranged as consecutive blocks.

1. Concepts

Slices are an abstracted data structure, and manipulated with an underlying array. It has three fields containing metadata that help Go to manage an array underneath (Illustration below). These three fields are pointers to the array, length, and available capacity of slices . Length and strength will have a few differences I will mention in the next section.

2. Declare and initialize

There are several ways to declare and create slices in Go . Knowing the capacity of slices in advance will help you determine how to create slices . You can create slices using the make function supported by Go . When you use make , you must specify the length of the slices

When you specify the length of slices when declaring, the available capacity is equal to the length of slices when declared. You can also specify separate lengths and availability.

When you specify separate lengths and availability. You can create a slices with available space in the array below, but won’t be able to access it. This means, you can only access 3 elements, but below can have 5 elements (above example). 2 elements that are not linked to the length of slices can be combined to make slices using those elements. Go does not allow you to create a slices that have the ready capacity of less than the length.

You can use slice literal to initialize a slice. It is the same way you initialize an array, but you won’t need to specify a value inside the [] operator. The available length and capacity initialization values ​​are based on the number of slices elements.

When using slice literal, you can set an initial value for the length and available capacity. To do this, you simply initialize the index represented to make the length and capacity available. Check out the example below

Remember, if you are initializing the value inside the [] operator, you are creating an array. If you do not specify a value, you are creating a slice .

Sometimes in your program it may be necessary to declare a nil slice. A slice with nil value is initialized as follows:

You could also initialize an empty slice like this:

An empty slice contains no elements in the array below. Empty slices are useful when you want to represent a specific model or collection, for example the results of a query from a database.

3. Work with slices

Now that everyone knows how to declare and instantiate a slice , now we will learn how to use the slice in a program.

Just like arrays, we can also use the assignment operator ( = ) to assign a value to an element in the slice .

You can also create a new slice by taking a slice

With the way to create the slice above, we will have 2 slices sharing the same array below. However each slice will look different with the underlying array.

Any new slice created in the above way will calculate the available length and capacity using the following formula:

Remember, you have two slices sharing the same array below. If you make any changes to the array below, both slices will receive that change.

After the assignment is done, the value of the second slice is also changed. You can see the illustration below:

As you always remember, you can only use the index to access individual elements with index up to the length of the slice .

Growing slices One of the advantages of slices over arrays is that you can simply add the number of elements to the slice . Go has provided an append function that makes it possible for you to do just that.

To use the append , you need an original slice and a value that you are about to add. the append will return you a new slice with the changes. The append will always increase the length of the new slice , but the capacity can be changed or not, depending on the capacity of the original slice .

Once done, append . Slices and the array below will be illustrated as follows:

Since newSlice still has redundancy ( ie the slice length is less than the capacity ) capacity, when the append is active, it merges the available elements and the length of the slice and assigns a value. Since the original slice shares the array below, it will also see a change in the element with an index of 3.

When the slice is exhausted ( ie the length of the slice is equal to the capacity ) capacity in the array below, the append creates a new array underneath, copies the value it is referencing, and assigns a new value. Please see the picture below to understand better:

Although there is quite a lot of content I want to convey in this article, but at this point I think the knowledge content is quite long and is also basic enough for you to use slices in Go . If you have any questions, please comment below, I will try to explain to you the easiest way to understand. Thank you everyone for reading my article

Link to the original article:

Share the news now

Source : Viblo