Go Reference ยท Collections: sort, slices, maps
Go slices.All(), slices.Values() and slices.Collect() functions
Reference for Go slices.All, Values, Backward, Collect, AppendSeq, Sorted and Chunk: convert between slices and iterators (Go 1.23) with range-over-func.
Go 1.23 added iterators to the language: a function of type iter.Seq[V] or iter.Seq2[K, V] that a for range loop can drive. The slices package connects slices to this world. slices.All(s) yields index and value pairs, slices.Values(s) yields only the values, and slices.Backward(s) goes from the end to the start.
In the other direction, slices.Collect(seq) gathers the values of an iterator into a new slice, slices.AppendSeq(s, seq) appends them to an existing one, and slices.Sorted(seq) collects and sorts in one step.
for i, v := range slices.All([]string{"a", "b"}) { fmt.Println(i, v) }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func All[Slice ~[]E, E any](s Slice) iter.Seq2[int, E] func Values[Slice ~[]E, E any](s Slice) iter.Seq[E] func Backward[Slice ~[]E, E any](s Slice) iter.Seq2[int, E] func Collect[E any](seq iter.Seq[E]) []E func AppendSeq[Slice ~[]E, E any](s Slice, seq iter.Seq[E]) Slice func Sorted[E cmp.Ordered](seq iter.Seq[E]) []E func SortedFunc[E any](seq iter.Seq[E], cmp func(E, E) int) []E func Chunk[Slice ~[]E, E any](s Slice, n int) iter.Seq[Slice]
| Name | Description |
|---|---|
s |
The source slice. |
seq |
An iterator yielding single values. |
n |
Chunk size for Chunk. |
cmp |
Three-way comparison for SortedFunc. |
Returns: All, Values, Backward and Chunk return iterators; Collect, Sorted and SortedFunc return a new slice; AppendSeq returns the extended slice.
Panics: Chunk panics if n < 1.
Usage notes
- Version: these functions need Go 1.23 or later; the language feature (range over function) arrived in the same release.
- Iterators are lazy.
AllandValuesread the slice while you loop, so changing elements during the loop is visible. Appending to the slice inside the loop does not extend the iteration, because the loop uses the slice value from the call. range sis simpler for plain slices. UseAll/Valueswhen an API expects aniter.Seq, or when you want to pass a slice into a function written for iterators, such asmaps.Collectorslices.Sorted.Collectallocates a new slice and returns nil for an empty iterator. UseAppendSeqto reuse capacity.Chunkshares memory: each chunk is a sub-slice of the original with its capacity clipped to its length, so appending to a chunk does not overwrite the next one.- Backward is the idiomatic way to loop from the end; the index it yields is the real index in the slice.
- Pairs with
maps:slices.Sorted(maps.Keys(m))is the standard way to get sorted map keys;slices.Collect(maps.Values(m))gives the values. - Early exit works:
breakmakes the iterator'syieldreturn false, and well-written iterators stop immediately.