What are Slices in Golang?

What are Slices in Golang?'s picture

If you have some experience in other programming languages like C or Java, you would know about arrays. To get things started, just imagine slices to be like arrays, with an abstraction layer and plethora of extra features. They are one of the key data types in Golang, dynamically sized and more powerful than arrays. In this blog, we will take a detailed look at slices and by the end of it, you should have no issues working with slices.


Let’s begin with a program, shall we?

Creating a Sample Program in Slices

  • We create GuitarBrands slice. Remember, when you create the slice, you do not have to specify the values that will be contained in it. If you define the values, then it’s a simple array.
  • Since, you do not define the values, you just announce that this is a string.

var GuitarBrands = [] string{}

When you insert {}, you are initializing it.

  • Now, if you are interested in knowing what type of data this is, then you type the code accordingly as shown below. And when you run it in the terminal, it displays

‘Type of GuitarBrands is [] string’

Creating a Sample Program in Slices
  • In slices, we can include as many values as we want, and it expands the memory automatically. So, what we will do next is add some extra values.
  • Now, when we try to add further values, we rely on a method known as append. For this, you have to specify the slice in which you wish to add the data. Following this, you have to add the data. Take a look at the example below to understand it better.
Creating a Sample Program in Slices go

Setting the Range

Now, we move on to a fun part. In this section, we will take a look at further application of append and set a range on the slice. In this case, first, you should have a look at the program, then we will get into the explanation.


Setting the Range go

Explanation:

  • This is just slicing up the slice. When we set the range, [1:3], what we are doing is that we are starting our counting process from position 1, which is Ibanez. Thus, we are excluding the element at 0, which is Schecter.
  • Now, what you have to notice here is that the last range is always non-inclusive. This means that the pointer will go from Ibanez to Charvel, but Charvel will not be counted. What the program will display is that [Ibanez Fender].
  • This concept will come in handy when you design to-do apps or deal with databases. There will be long slices and if .you have to delete just one value from the total, then you can use this theory there and get the result.

Additional Things You Must Know in Slice

Using make()

It is a different way of creating slices. Here, you first have to define the type of data you require, following which you have to state the size.Let us have a look at the program.

Using make()

Explanations:

Here registrationnumbers is the slice. And we are using make (). The slice is of type int and it consists of 4 elements. So, we assign the different values as per the position. Now, if you plan to include further numbers, you can do so by using the append (). And when you execute the program, you get output

[111 213 139 413 499 387 87]. So the slice initially had a size for 4 elements. Upon requesting memory allocation, it was increased further. As you can see, it saves a lot of memory, time and performance optimization is great.

How about Sorting the Numbers?

Yes, you can easily do that in slices with the help of sort package. If you wish to arrange the numbers in ascending order, all you have to do is use sort.Ints

How about Sorting the Numbers?

Once you understand the working of slices, you will be able to understand the databases easily. So, it is imperative that you have a thorough working knowledge of slices. And the best way to enhance your understanding is to code and execute programs. So, get cracking!


Build Your Golang Team