Skip to content
Tutorials Center
My learning

Topics

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.

Go
for i, v := range slices.All([]string{"a", "b"}) {
	fmt.Println(i, v)
}

Try it

Go Slices to iterators and back

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.

Go Your own iterators with Collect and Chunk

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

Go
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. All and Values read 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 s is simpler for plain slices. Use All/Values when an API expects an iter.Seq, or when you want to pass a slice into a function written for iterators, such as maps.Collect or slices.Sorted.
  • Collect allocates a new slice and returns nil for an empty iterator. Use AppendSeq to reuse capacity.
  • Chunk shares 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: break makes the iterator's yield return false, and well-written iterators stop immediately.