Preamble
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
1 2 3 4 | <span class="token comment">// Tạo một slices string</span> <span class="token comment">// Bao gồm độ dài và sức chứa của 5 phần tử</span> slice <span class="token operator">:=</span> <span class="token function">make</span> <span class="token punctuation">(</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">string</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">)</span> |
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.
1 2 3 4 | <span class="token comment">// Khởi tạo một slices integer</span> <span class="token comment">// Bao gồm độ dài là 3 và sức chứa sẵn sàng là 5</span> slice <span class="token operator">:=</span> <span class="token function">make</span> <span class="token punctuation">(</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">,</span> <span class="token number">5</span> <span class="token punctuation">)</span> |
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.
1 2 3 4 5 6 7 8 | <span class="token comment">// Tạo một slice string có 5 phần tử.</span> <span class="token comment">// Bao gồm độ dài và sức chứa sẵn sàng của 5 phần tử</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">string</span> <span class="token punctuation">{</span> <span class="token string">"Red"</span> <span class="token punctuation">,</span> <span class="token string">"Blue"</span> <span class="token punctuation">,</span> <span class="token string">"Green"</span> <span class="token punctuation">,</span> <span class="token string">"Yellow"</span> <span class="token punctuation">,</span> <span class="token string">"Pink"</span> <span class="token punctuation">}</span> <span class="token comment">// Tạo một slice integer có 3 phần tử.</span> <span class="token comment">// Bao gồm độ dài và sức chứa sẵn sàng của 3 phần tử</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">}</span> |
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
1 2 3 4 | <span class="token comment">// Tạo một slice string</span> <span class="token comment">// Khởi tạo 100 phần tử với string empty</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">string</span> <span class="token punctuation">{</span> <span class="token number">99</span> <span class="token punctuation">:</span> <span class="token string">""</span> <span class="token punctuation">}</span> |
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:
1 2 3 | <span class="token comment">// Khai báo một nil slice integer</span> <span class="token keyword">var</span> slice <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> |
You could also initialize an empty slice like this:
1 2 3 4 5 6 | <span class="token comment">// sử dụng make để tạo một slice có kiểu integer rỗng</span> slice <span class="token operator">:=</span> <span class="token function">make</span> <span class="token punctuation">(</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">,</span> <span class="token number">0</span> <span class="token punctuation">)</span> <span class="token comment">// sử dụng slice literal để tạo một slice kiểu integer rỗng</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> |
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 .
1 2 3 4 5 6 | <span class="token comment">// Tạo mới một slice kiểu integer</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">,</span> <span class="token number">40</span> <span class="token punctuation">,</span> <span class="token number">50</span> <span class="token punctuation">}</span> <span class="token comment">// Thay đổi giá trị của phần tử có index là 1</span> slice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">25</span> |
You can also create a new slice by taking a slice
1 2 3 4 5 6 | <span class="token comment">// Tạo mới một slice kiểu integer.</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">,</span> <span class="token number">40</span> <span class="token punctuation">,</span> <span class="token number">50</span> <span class="token punctuation">}</span> <span class="token comment">// Tạo mới một slice có độ dài là 2 và sức chứa sẵn sàng là 4.</span> newSlice <span class="token operator">:=</span> slice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">]</span> |
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:
1 2 3 4 5 6 7 8 | slice <span class="token punctuation">[</span> i <span class="token punctuation">:</span> j <span class="token punctuation">]</span> và sức chứa sẵn sàng là k <span class="token punctuation">.</span> Độ dài <span class="token punctuation">:</span> j <span class="token operator">-</span> i Sức chứa sẵn sàng <span class="token punctuation">:</span> k <span class="token operator">-</span> i Vậy trong trường hợp ví dụ trên của mình sẽ là slice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">]</span> Độ dài <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">2</span> Sức chứa sẵn sàng <span class="token punctuation">:</span> <span class="token number">5</span> <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">=</span> <span class="token number">4</span> |
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.
1 2 3 4 5 6 7 8 9 | <span class="token comment">// Tạo một slice kiểu integer.</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">,</span> <span class="token number">40</span> <span class="token punctuation">,</span> <span class="token number">50</span> <span class="token punctuation">}</span> <span class="token comment">// Tạo mới một slice.</span> newSlice <span class="token operator">:=</span> slice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">]</span> <span class="token comment">// Thay đổi giá trị ở index là 2 ở slice.</span> newSlice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">35</span> |
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 .
1 2 3 4 5 6 7 8 9 10 | <span class="token comment">// Tạo mới một slice integer.</span> <span class="token comment">// Độ dài và capacity là 5.</span> slice <span class="token operator">:=</span> <span class="token punctuation">[</span> <span class="token punctuation">]</span> <span class="token builtin">int</span> <span class="token punctuation">{</span> <span class="token number">10</span> <span class="token punctuation">,</span> <span class="token number">20</span> <span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">,</span> <span class="token number">40</span> <span class="token punctuation">,</span> <span class="token number">50</span> <span class="token punctuation">}</span> <span class="token comment">// Tạo mới một slice từ slice gốc.</span> <span class="token comment">// Độ dài là 2 và capacity là 4.</span> newSlice <span class="token operator">:=</span> slice <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">:</span> <span class="token number">3</span> <span class="token punctuation">]</span> newSlice <span class="token operator">=</span> <span class="token function">append</span> <span class="token punctuation">(</span> newSlice <span class="token punctuation">,</span> <span class="token number">60</span> <span class="token punctuation">)</span> |
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: https://chiasekienthuc.netlify.app/blog/slices