Go Reference ยท Collections: sort, slices, maps
Go slices.Reverse(), slices.Max() and slices.Min() functions
Reference for Go slices.Reverse, Max, Min, MaxFunc, MinFunc, Equal and Compare: reverse in place, find extremes, compare slices, empty-slice panics and NaN rules.
Three small helpers from the slices package replace loops you have written many times. slices.Reverse(s) reverses a slice in place. slices.Max(s) and slices.Min(s) return the largest and smallest element of a slice of ordered values. When the elements are not ordered types, such as structs, MaxFunc and MinFunc take a three-way comparison function.
Closely related are slices.Equal and slices.Compare, which compare two slices element by element.
s := []int{1, 2, 3} slices.Reverse(s) // [3 2 1]
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 Reverse[S ~[]E, E any](s S) func Max[S ~[]E, E cmp.Ordered](x S) E func Min[S ~[]E, E cmp.Ordered](x S) E func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E func Equal[S ~[]E, E comparable](s1, s2 S) bool func Compare[S ~[]E, E cmp.Ordered](s1, s2 S) int
| Name | Description |
|---|---|
s, x |
The slice to reverse or to search for an extreme. |
cmp |
Three-way comparison: negative if a < b, zero if equal, positive if a > b. |
s1, s2 |
Slices to compare. |
Returns: Reverse returns nothing. Max, Min, MaxFunc and MinFunc return the element. Equal returns a bool. Compare returns -1, 0 or +1 (lexicographic comparison; a prefix is smaller than the longer slice).
Panics: Max, Min, MaxFunc and MinFunc panic on an empty slice. They do not return a zero value.
Usage notes
- Reverse is in place and returns nothing. It touches every element once,
O(n), and works on a sub-slice to reverse only a window. - Check for emptiness first when the slice may be empty:
if len(s) > 0 { m := slices.Max(s) }. - Ties:
MaxFuncandMinFuncreturn the first element among equal ones. In the example,Romeis reported, notKyiv. - Floats and NaN: if any element is NaN,
MaxandMinreturn NaN. Filter NaNs out beforehand if that is a risk. - Position of the maximum: these functions return the value, not the index. Use
slices.Index(s, slices.Max(s)), or a manual loop if you need both in one pass. Equaltreats nil and empty slices as equal and requires equal lengths. Useslices.EqualFuncfor non-comparable element types.- Built-in
maxandmin(Go 1.21) take individual arguments,max(a, b, c); theslicesversions take a whole slice. - Sorting is not needed to find extremes;
Maxis a singleO(n)scan.