Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Remove first element of slice (shift, pop(0))

  • shift
package main

import (
	"fmt"
)

func main() {
	dwarfs := []string{"Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey"}
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))

	// remove first element
	first := dwarfs[0]
	fmt.Println(first)

	dwarfs = dwarfs[1:]
	fmt.Println(dwarfs)
	fmt.Println(len(dwarfs))
	fmt.Println(cap(dwarfs))

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