Tsallaka zuwa abun ciki
Justice Tech School

Slices In Depth

Remove and insert with append and re-slicing. The slices package adds Sort, Contains, Index, Insert, Max and more. Slices share their backing array, so copy when you need independence.

Example
package main

import (
	"fmt"
	"slices"
)

func main() {
	s := []int{5, 2, 9, 1}
	s = append(s[:1], s[2:]...)
	fmt.Println(s)
	s = slices.Insert(s, 1, 7)
	slices.Sort(s)
	fmt.Println(s, slices.Contains(s, 9), slices.Index(s, 7), slices.Max(s))
}

Result

[5 9 1]
[1 5 7 9] true 2 9

Exercise

Sort a slice.

slices.(s)

Quiz Test

How do you remove index i from s?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.