typestar

The slices package in Go

Generic helpers that used to be hand-written in every project.

func sliceHelpers() []string {
    langs := []string{"rust", "go", "python", "sql"}

    slices.Sort(langs)
    position, found := slices.BinarySearch(langs, "python")
    reversed := slices.Clone(langs)
    slices.Reverse(reversed)

    return []string{
        fmt.Sprint(langs),
        fmt.Sprint(slices.Contains(langs, "go"), slices.Index(langs, "sql")),
        fmt.Sprint(position, found),
        fmt.Sprint(reversed, slices.Max(langs)),
        fmt.Sprint(slices.Equal(langs, reversed)),
    }
}

How it works

  1. Contains, Index and Sort cover the common cases.
  2. BinarySearch returns the position and whether it was found.
  3. Delete and Insert return a new slice header.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
436
Tokens
128
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 73 seconds.

Type this snippet

Step 1 of 5 in Collections & text, step 9 of 26 in Standard library & project layout.

← Previous Next →