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
Contains,IndexandSortcover the common cases.BinarySearchreturns the position and whether it was found.DeleteandInsertreturn a new slice header.
Keywords and builtins used here
funcreturnstring
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.
Step 1 of 5 in Collections & text, step 9 of 26 in Standard library & project layout.