DEV Community

Cover image for Day 11 of #30DaysOfCode | Arrays and Slices In Golang (Part 2)
Shubham Saurav
Shubham Saurav

Posted on

Day 11 of #30DaysOfCode | Arrays and Slices In Golang (Part 2)

You are better than you think you are. I started this #30DaysOfCode challenge and I am going through it. I am learning every day and actually achieving something. People say that Consistency is the key to success. It is true and you can be consistent too. There is nothing special in me. We are more or less the same. You can also do it. You can also start a challenge like this and finish it. You have got the power, the energy and the passion. You just need to recognise it. And if you need any help then comment below and I'll do my best to help you.

Now let's move on to the post.

Today is Day 11 of the challenge and I would like to share more about the array and slices in Golang. In the last post, we learnt about what arrays and slices are and the difference between them. In this one, we will learn how we can define them and then some of the operations that we can perform on them.

Firstly, let's just take a look at the example below before reading any further.

Array and Slice Example

Previously, we learned that we cannot add more element to an array but we can add more element to the slice and we can do this using the append() function. In the append function, we need to pass the slice in the first parameter and in the second parameter, we need to pass the value or values that we want to append. The append function will add the elements to the slice and return a new slice. You can store the new slice in the variable and then can use it later. Take a look at the example to understand how it works.

The second operation I wanna talk about is changing the data. This one is very simple and works with both arrays and slices. Just write the slice or array name and pass the index in the square bracket and then use the '=' to change the data. It's very simple and easy. Just take a look at the example and you will get it.

The third thing I wanna talk about is reading the data of a single element. Again, it's very simple. Just use the array or the slice name and pass the index of the element you want to read and you will be able to read the data of that specific element.

The last thing I wanna talk about is counting the length of the arrays or slices. This is also very easy to do. You can use the len() function to count the length of an array or a slice. Just call the len() and pass the array or slice as the parameter. This will return the length of the array or slice that's passed in. Take a look at the example to learn how to do it.

These are the common operations that you can perform with these collection types and if you wanna learn more about them then let me know in the comments below.

Alright! That's enough for this post and I'll see you in the next one. Bye.

Connect With Me:
Youtube: ShubhamSauravYT.
Twitter: @hiShubhamSaurav
Instagram: @hiShubhamSaurav
Facebook: @hiShubhamSaurav

Top comments (0)