Mahdi.Kh
September 21, 2026
In this part, we get familiar with the slice data type, and look at how to use it along with the operations related to it.
The slice data type is an abstraction built on top of the array data type, meaning slice is implemented using an array. So before getting familiar with slice, we need a basic familiarity with arrays.
To define an array, we just need to specify its length and the type of its items:
The values inside an array are placed sequentially in RAM.
For a few reasons, arrays aren't commonly used:
[4]int can't be assigned to a variable whose type is [5]int. (The types [4]int and [5]int are distinct and incompatible!)Unlike arrays, defining a slice doesn't require specifying its length! To declare a slice, we use a pair of square brackets [ ] together with the data type we want:
When declaring a slice, we do the same thing as with an array, except we leave the square brackets [ ] empty.
In Go, every data type has a default value called the zero value, which is assigned when a variable is declared without an initial value. This feature means variables in Go always have a valid value, which prevents errors related to initialization. A slice's zero value is nil. You can allocate memory to a slice using the make function.
Note: unlike slices, arrays don't need explicit initialization; an array's zero value is a ready-to-use array whose items themselves hold the zero value!
Another way to create a slice is using := and the make function:
If you know, before creating the slice, the values that will go inside it, you can use a slice literal:
So, so far we've learned there are 3 ways to create a slice:
make function:= and the make functionA slice is a data structure made up of three parts
When we declare a slice, its zero value is nil. This means the pointer in this slice doesn't point to anything, and the len and cap values are zero.
When we use the make function, it first creates a slice, then creates an array of the given size (cap), and sets the slice's pointer to point to that array's first element; it then sets the len and cap values, and finally returns the created slice as the result.
The make function accepts three input parameters. The first two are required, and the third is optional. The first parameter represents the data type, the second specifies len, and the third specifies cap.
You can find a slice's length and capacity using the len and cap functions, respectively:
If a slice's length (len) is 5, that means we'll have access to indexes 0 through 4 (5 values in total, from 0 to 4):
If we try to read or set elements outside the len range, we'll get an error:
To add one or more values to the end of a slice, you can use the append function.
The append function adds the given values to the end of the array and increases its length by the number of values added. If the slice's length (len) would exceed its capacity (cap), the array the slice's pointer points to won't have room to store the new values, and in that case, the append function does the following:
To copy values from one slice to another, you can use the copy function.
The copy function copies values from the second parameter into the first parameter, up to the first parameter's length, and returns a number equal to how many items were copied.
You can slice an array or slice again into smaller parts; to do this, you can use [start:end]. The resulting slice includes every item from index start up to end, but doesn't include end itself.
By slicing an array or slice, the new slice's pointer will point to the previous array, or to wherever the original slice's pointer points. This means if we change a value in the newly created slice, it will also change in the original variable.
The start and end indexes can be left unspecified. In that case, the start index is taken to be 0, and the end index is taken to be the original array's or slice's length.
This post's content was written based on slices-intro from Go's official website.
Previous part: The String Data Type | Golang basics
Next part: Loops | Golang basics