Golang: Slice

By Xah Lee. Date: . Last updated: .

Slice is like array but length can be changed.

Syntax of Slice Type

[]T → syntax for the type of slice with each slot of type T.

For example, var ss []int means variable ss is a slice, with slots being type int.

Recall, syntax of array type is :

[n]T → syntax for the type of array with n slots and each slot of type T.

〔►see Golang: Basic Datatypes

package main

import "fmt"
import "reflect"

func main() {

        // declare var of type “3 items array”
        var aa [3]int

        // declare var of type slice
        var ss []int

        fmt.Println(reflect.TypeOf(aa)) // [3]int
        fmt.Println(reflect.TypeOf(ss)) // []int

}

Literal Expression of Slice

[]type{v1, v2 …} → create a slice with values.

package main

import "fmt"
import "reflect"

func main() {

        // array
        var aa = [3]int{9, 2, 6}

        // slice
        var ss = []int{9, 2, 6}

        fmt.Println(reflect.TypeOf(aa)) // [3]int
        fmt.Println(reflect.TypeOf(ss)) // []int

}

Create slice with 「make」

You can create a slice with make. It lets you specify how many items to begin with, and capacity for growth.

capacity_m defaults to the value of count_n

package main

import "fmt"

func main() {
        var s = make([]int, 3) // 3 items of int
        fmt.Println(s)         // [0 0 0]
}
package main

import "fmt"

func main() {
        var s = make([]int, 3, 9) // 3 items of int, capacity of 9
        fmt.Println(s)            // [0 0 0]
}

length of slice

len(s) → returns the length of slice s.

Take a slice of slice

s[a:b] → returns a slice of s from index a to b. The a is included, The b is excluded.

package main

import "fmt"

func main() {
        var s = []int{1, 2, 3, 4, 5, 6, 7}
        fmt.Println(s) // [1 2 3 4 5 6 7]

        // take a slice of values. include first index, exclude second index
        fmt.Println(s[1:3]) // [2 3]
}

Append to Slice

package main

import "fmt"

func main() {

        var ss = []int{3, 4, 5}

        var s2 = append(ss, 8, 9)

        fmt.Println(s2) // [3 4 5 8 9]
}

Nested Slice

Slices can be nested. Just declare that the slots are also type slice.

package main

import "fmt"

func main() {
        // x is slice of slice of int
        var x [][]int
        fmt.Println(x) // []
}

Nested slice with values.

package main

import "fmt"

func main() {

        // y is slice of slice of int
        var y = [][]int{{3, 4}, {7, 8, 9}, {1, 2}}

        fmt.Println(y) // [[3 4] [7 8 9] [1 2]]
}

Creating nested slice with make.

package main

import "fmt"

func main() {

        var sl = make([][]string, 2)

        sl[0] = []string{"a", "b"}
        sl[1] = []string{"x", "y", "z"}

        fmt.Println(sl) // [[a b] [x y z]]
}

Getting and setting values.

package main

import "fmt"

func main() {

        var ss = make([][]int, 2)

        ss[0] = []int{1, 2}
        ss[1] = []int{3, 4}

        ss[1][1] = 5

        fmt.Println(ss) // [[1 2] [3 5]]
}

zero value of slice is nil

package main

import "fmt"

func main() {
        var z []int
        fmt.Println(z, len(z), cap(z))
        if z == nil {
                fmt.Println("nil!")
        }
}

loop thru slice

package main

import "fmt"

func main() {

        // slice
        var s = []int{9, 2, 8, 61}

        for i := 0; i < len(s); i++ {
                fmt.Println(i, s[i])
        }
}

// 0 9
// 1 2
// 2 8
// 3 61

Reference

The Go Programming Language Specification - The Go Programming Language#Slice_types

The Go Programming Language Specification - The Go Programming Language#Appending_and_copying_slices