Slice


The only difference you can see when we create a slice is that we don't explicitely say its size and we also don't put the 3 dots ... in the square bracket.

There is also a cap function that returns the size of the underlying array.

We can access the elements of a slice using the postfix square-bracket notation. Just as with arrays.


examples/slice/slice.go
package main

import (
    "fmt"
)

func main() {
    var dwarfs = []string{"Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey"}

    fmt.Println(dwarfs)
    fmt.Println(dwarfs[0])
    fmt.Println(len(dwarfs))
    fmt.Println(cap(dwarfs))
}

[Doc Grumpy Happy Sleepy Bashful Sneezy Dopey]
Doc
7
7