Slices in Golang

Slices represent a sequence of elements of the same type of variable length. Unlike arrays, the length in slices is not fixed and can change dynamically, That is, you can add new items or remove existing ones.

Define a Slice

A slice is defined in the same way as an array, except that it does not specify a length:

var names []string

You can also initialize the slice with the following values:

var names = []string{"Mead", "Pira", "Redi"}

Slice elements are accessed in the same way as array elements (by index), and we can also iterate over all elements using a for loop:

package main

import "fmt"

func main() {
    var names = []string{"Mead", "Pira", "Redi"}
    fmt.Println(names[0]) // Mead

    for _, value := range names {
        fmt.Println(value)
    }
}

if we run this we should get an output like this:

Mead
Mead
Pira
Redi

like what we did with arrays, we can change an element with an index:

names[0] = "techieclues"

Using Make Function

Using the make() function, you can create a slice of several elements that will have default values:

var names []string = make([]string, 3)

Adding to a Slice

To add to a slicer, the built-in append(slice, value) function is used. The first parameter of the function is the slice to which you want to add, and the second parameter is The value you want to add. The result of the function is an enlarged slice.

names := []string{"Rita", "Mead", "Lora"}
names = append(names, "Hanna")

Slice Operator

let's take this example:

package main

import "fmt"

func main() {
    authors := [6]string{"Mead", "Rita", "Marc", "Pirra", "John", "Essa"}
    react := authors[1:3] // Slice from index 1 to 2 (3-1)
    python := authors[:5] // Slice from the beginning up to index 4 (5-1)
    golang := authors[4:] // Slice from index 4 to the end

    fmt.Println(react)
    fmt.Println(python)
    fmt.Println(golang)

}

The slice operator in Go, defined as s[a:b], is used to create a new slice of elements from a sequence s. 

let's break down what we did in the example above to understand more:

  • s: The initial sequence from which the elements are taken, An array, a pointer to an array, or another slice can be used
  • a: The starting index and it's included
  • b: The end index but it's not included

As a result, we get a slice from an up to b but b is not included:

[Rita Marc]
[Mead Rita Marc Pirra John]
[John Essa]

Delete an Item

We do have four built-in functions that can used within slices, to add an element we use append() function, to compare two slices we use equals(), copy() is used to copy a slice and the last one is len() to get the length of a slice.
 
But what should I do if I need to delete a specific item? In this case, we can combine the append function and the slice operator:

package main

import "fmt"

func main() {
    authors := []string{"Mead", "Rita", "Marc", "Pirra", "John", "Essa"}
    var x = 4
    authors = append(authors[:x], authors[x+1:]...)
    fmt.Println(authors) 
}

The output will be like this:

[Mead Rita Marc Pirra Essa]

let's understand what we did here:

  • authors[:x]: We take the beginning of the slice up to but do not include x (4). 
  • authors[x+1:]: We take all the element from x+1 (5) up to the end. 

As a result, we do have two slices:

 authors[:x] = [Mead Rita Marc Pirra]

 authors[x+1:] = [Essa]

To concatenate the two slices (the part before and after the element at index x) we use the function append().