Golang Slices

In this tutorial, we will learn how to work with slices in Golang with examples.

A slice is a dynamically sized, flexible view into the elements of an array. A slice can grow and shrink within the bounds of the underlying array. A slice does not store any data, it just describes a section of the array.

Go - create slice simple example

A slice can be created with a slice literal.

In the example, we create two slices:

package main

import "fmt"

func main() {
    var s1 = []int{2, 5, 6, 7, 8}

    s2 := []int{3, 5, 1, 2, 8}

    fmt.Println("s1:", s1)
    fmt.Println("s2:", s2)
}

Output:

s1: [2 5 6 7 8]
s2: [3 5 1 2 8]

Go - slice make function

We can use the make built-in function to create new slices in Go.

package main

import "fmt"

func main() {

    vals := make([]int, 5)

    fmt.Println("vals: ", vals)

    vals[0] = 1
    vals[1] = 2
    vals[2] = 3
    vals[3] = 4
    vals[4] = 5

    fmt.Println("vals: ", vals)
}

Output:

vals:  [0 0 0 0 0]
vals:  [1 2 3 4 5]

Go - slice length and capacity example

The len() function returns the number of elements in the slice.

The cap() function returns the capacity of the slice. The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.

In the example, we print the size and the capacity of two slices:

package main

import "fmt"

func main() {

    vals := make([]int, 10, 15)

    n := len(vals)
    c := cap(vals)

    fmt.Printf("The size is: %d\n", n)
    fmt.Printf("The capacity is: %d\n", c)

    vals2 := vals[0:8]
    n2 := len(vals2)
    c2 := cap(vals2)

    fmt.Printf("The size is: %d\n", n2)
    fmt.Printf("The capacity is: %d\n", c2)
}

Output:

The size is: 10
The capacity is: 15
The size is: 8
The capacity is: 15

Go - slice iteration example

With for loops, we can iterate over slice elements in Go.

In the example, we iterate over a slice of days with for/range statements.

package main

import "fmt"

func main() {

    days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

    for idx, day := range days {

        fmt.Println(idx, day)
    }
}

Output:

0 Monday
1 Tuesday
2 Wednesday
3 Thursday
4 Friday
5 Saturday
6 Sunday

In the following example, we iterate a slice with a classic for loop.

package main

import "fmt"

func main() {

    days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}

    for i := 0; i < len(days); i++ {

        fmt.Println(days[i])
    }
}

Output:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Go - slice append example

Go slice append - The built-in append() function appends new elements to the slice.

In the example, we append new elements to a slice, which already has two elements.

package main

import "fmt"

func main() {

    vals := make([]int, 2)

    fmt.Printf("slice: %v; len: %d; cap: %d \n", vals, len(vals), cap(vals))

    fmt.Println("---------------------------")

    vals = append(vals, 10)
    vals = append(vals, 20)
    vals = append(vals, 30)
    vals = append(vals, 40, 50, 60)

    fmt.Printf("slice: %v; len: %d; cap: %d \n", vals, len(vals), cap(vals))
}

Output:

slice: [0 0]; len: 2; cap: 2 
---------------------------
slice: [0 0 10 20 30 40 50 60]; len: 8; cap: 8

Go - slice copy example

The Go provides the built-in copy() function copies a slice.

func copy(dst, src []T) int

The function returns the number of elements copied.

In the example, we copy a slice of integers.

package main

import "fmt"

func main() {

    vals := []int{1, 2, 3, 4, 5}

    vals2 := make([]int, len(vals))

    n := copy(vals2, vals)

    fmt.Printf("%d elements copied\n", n)

    fmt.Println("vals:", vals)
    fmt.Println("vals2:", vals2)
}

Output:

5 elements copied
vals: [1 2 3 4 5]
vals2: [1 2 3 4 5]

Go - slice remove element example

There is no built-in function to remove items from a slice. We can do the deletion with the append() function.

In the example, we delete an element and then two elements from the slice.

package main

import (
    "fmt"
)

func main() {

    days := []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
    fmt.Println(days)

    days = append(days[1:2], days[2:]...)
    fmt.Println(days)

    days = append(days[:2], days[4:]...)
    fmt.Println(days)
}

Output:

[Monday Tuesday Wednesday Thursday Friday Saturday Sunday]
[Tuesday Wednesday Thursday Friday Saturday Sunday]
[Tuesday Wednesday Saturday Sunday]

Golang Related Tutorials

Comments