Hey beautiful person! How are you doing today? Hopefully, you are doing great and having loads of fun. I myself am enjoying the #30DaysOfCode challenge and having loads and loads of fun learning new things every day. Today is the Day 10 of challenge and I am super happy that I have completed the 33.33% of the challenge and I have a lot to share with you so read on.
Today, I dived a bit deeper into arrays and slices in Golang and I would like to share the basic with you. The Array and Slice are key data type in Go. They are basically a collection type. They are used to store a collection of data. They are similar to List in Python, Array in Swift, Java, C++ and many other programming languages.
The arrays and slices are used to store a collection of similar-related data. Suppose that you want to store the names of all the friends you have. Then you would use an array or slice to store the collection of names of your friends. Now, let's talk about each of the types and the key differences between them.
A look into Arrays in Golang
Arrays are a collection data type in Go Programming Language. Arrays in Go have fixed length and once they are defined you cannot add more element into it. They can be used to store data of similar types. Storing similar type means that you can only store data of similar data type. For eg: you can store a collection of int, or a collection of float32, or a collection of string in arrays but you can't store a mixed collection of int, float32 and string.
Arrays are also a value type. Being a value type means that the variable holds the value in its memory space. They don't store the address where the value is stored. So, when you copy an array then a completely new array is created which holds the copied value in its own memory space and not the address where the value is stored in memory. If you change the element of the one array then it will have no effect on the other array. The value in the other array won't be changed.
A look into Slices in Golang
Slices are a collection data type in Go Programming Language. The slices are a lot similar to arrays in Golang. They also can be used to store data of similar types. You can't also have a mixed collection of data in slices just like you can't have it in arrays. But there is a difference between the two.
Difference between Arrays and Slices in Golang
The main key difference between arrays and slices is that slices don't have a fixed length so, once they are defined you can add more elements to the slice whereas you cannot add more element to the array after you have defined it. Also, the slices are reference type in Go. So the slices don't store the values directly. They only store the address where the values are being stored. If you copy a slice then the new slice will copy the address where the value is stored and when you change an element then the values in both the slices will be changed.
Alright! that's enough for this post and in the next one, we will dive deeper into arrays and slices and will take a look at the operations that can be performed on them.
Connect With Me:
Youtube: ShubhamSauravYT.
Twitter: @hiShubhamSaurav
Instagram: @hiShubhamSaurav
Facebook: @hiShubhamSaurav
Top comments (0)