Photo by Honey Fangs on Unsplash

In this post we will see ,how we can perform various operations on the slices.

Unlike arrays we can append and extend the slices ,but first we will see different ways of initialization of slices and we will see different operations further.

Slice initialization

There are various ways to initialize the slice and below are four ways which are used (please refer initialization program for below):-

  1. Using the literals :-

As shown below “a” slice has been initialized with literals and here we haven’t defined any size separately (like we used to define in Arrays)

2. Initializing the slice with “make” function ,make function is having below syntax :-

func make([]T, len, cap) []T

In case the capacity parameter is not passed the capacity is equals to length , like in below example “b” slice is initialized using make function and as we can see in the program output it is having length 3 and capacity 3

3. Initializing the slice with capacity parameters helps to allocate the memory in advance which we might need while execution while performing with huge data. For small set of data it’s fine to omit the capacity ,but if we know in advance that data will be huge then we can keep the rough size of memory allocated in advance ,allocating memory (increasing capacity ) will be usually expensive during runtime ,will discuss further operations related to this in comping section.

In below example we can see that slice “c” is initialized with length 3 and capacity 100 (shown in the output)

4.Slices can be initialized/created by using “reference” of another slice ,as we can see in case of slice “d” ,it’s created with the slice operation and it is pointing to zeroth element of slice “a” ,here a[0:1] means element “till” index 1 ,but here we have to keep one thing in mind that if we change anything in the slice “a” or slice “d” it will be reflected in both slices “a” and “d” ,because it is pointing to same memory allocation.

Example for point 4(Slice from slice)

Output:-

Initialization Program

Output :-

Slice Extensions /Operations

1.Copy Function

func copy(dst, src []T) int

Copy function source slice to destination slice and returns the number of elements copied ,also this function works fine even if the underlying slices pointing to same slice, we will see different examples of these.

Copy Example

Copy Example

Output:-

In the above example source slice elements got copied into destination slice but since the capacity of destination slice is of only 3 elements ,so the first three elements only get copied destination slice as it do not increase the capacity of destination slice also it overwrites the existing elements with source elements.

Copy (Same Slice)

Copy with same slice

Output:-

In the above example ,source and destination refers to same slice and when we apply the copy operation it overwrites the original value as we can see in above program.

2.Append Function

func append(s []T, x ...T) []T

Append function extends the by re-allocating the capacity (internally it re-creates the slice and copies the elements ,if the capacity is less to accommodate new element(s)).We will see below few examples :-

Append example:-

Append example

Output:-

As we can see above in above example we can append single as well as multiple values in slice and we append slice as well using … (ellipsis).Besides this we have to see how internally it works when we append every time.

Append (with memory address details)

Append with address details

Output:-

Above shown an example where we are checking the memory address details after each operation ,so here we can clearly see that after “first” append capacity of slice increase by twice of original elements and after second append it do not require to extend the capacity further ,since it can accommodate the next element and this way append operation just adds the element at the end(no copy /recreation operation).

Summary

Conclusion is to better utilize this data structure ,we must know how in better way we can avoid re-creation of the slice and can effectively utilized existing memory (allocated to slice)

Happy Coding !

IT professional ,Poet