![]() |
VOOZH | about |
An array is a data structure. Similarly, in Golang we have slice which is more flexible, powerful, lightweight and convenient than array. As slice is more flexible than array therefore, its flexibility is determined in terms of its size. Just like an array, it has indexing value and length but its size is not fixed. When we declare a slice we do not specify the size of it.
Syntax:
[]mySlice;
OR
[]mySlice{};
OR
[]mySlice{input1, input2, .........input n}
Moreover, a slice and an array are connected with each other, in a slice, there is a referencing to an underlying array. And in a slice, we can store duplicate elements.
Example: Here, we will see how to remove the duplicate elements from slice. We have defined a function where we are passing the slice values and using the map function we are checking the duplicates and removing them.
Output:
[1 2 3 4 5 2 3 5 7 9 6 7] [1 2 3 4 5 7 9 6]
Explanation: