SoFunction
Updated on 2025-03-03

Detailed explanation of the usage of slice functions in the newly added slices package in Go 1.21

Definition is as follows:

func Max[S ~[]E, E ](x S) E

Returns the maximum value in x, if x is empty, panic. For floating point number E, if any element is NaN, the result is NaN. A simple example is as follows:

package main
import (
	"fmt"
	"math"
	"slices"
)
func main() {
	numbers := []int{0, 10, -1, 8}
	((numbers)) // 10 
	numbers2 := []float64{0, 10, -1, 8, ()}
	((numbers2)) // NaN
}

Definition is as follows:

func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E

Returns the maximum value in x, use the cmp function to compare elements, if x is empty, panic. If there are multiple largest elements after calculation based on the cmp function, return the first one. A simple example is as follows:

package main
import (
	"cmp"
	"fmt"
	"slices"
)
func main() {
	type Person struct {
		Name string
		Age  int
	}
	people := []Person{
		{"Gopher", 13},
		{"Alice", 55},
		{"Vera", 24},
		{"Bob", 55},
	}
	firstOldest := (people, func(a, b Person) int {
		return (, )
	})
	() // Alice
}

Definition is as follows:

func Min[S ~[]E, E ](x S) E

Returns the minimum value in x, if x is empty, panic. For floating point number E, if any element is NaN, the result is NaN. A simple example is as follows:

package main
import (
	"fmt"
	"math"
	"slices"
)
func main() {
	numbers := []int{0, 10, -1, 8}
	((numbers)) // -1
	numbers2 := []float64{0, 10, -1, 8, ()}
	((numbers2)) // NaN
}

Definition is as follows:

func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E

Returns the minimum value in x, use the cmp function to compare elements, if x is empty, panic. If there are multiple minimum elements after calculation based on the cmp function, return the first one. A simple example is as follows:

package main
import (
	"cmp"
	"fmt"
	"slices"
)
func main() {
	type Person struct {
		Name string
		Age  int
	}
	people := []Person{
		{"Gopher", 13},
		{"Alice", 55},
		{"Vera", 24},
		{"Bob", 55},
	}
	firstYoungest := (people, func(a, b Person) int {
		return (, )
	})
	() // Gopher
}

Definition is as follows:

func Replace[S ~[]E, E any](s S, i, j int, v ...E) S

Replace element s[i:j] with the given v and return the modified slice. If s[i:j] is not part of s, then panic. A simple example is as follows:

package main
import (
	"fmt"
	"slices"
)
func main() {
	names := []string{"Alice", "Bob", "Vera", "Zac"}
	names = (names, 1, 3, "Bill", "Billie", "Cat")
	(names) // [Alice Bill Billie Cat Zac]
}

Definition is as follows:

func Reverse[S ~[]E, E any](s S)

Invert the elements in the slice. A simple example is as follows:

package main
import (
	"fmt"
	"slices"
)
func main() {
	names := []string{"alice", "Bob", "VERA"}
	(names)
	(names) // [VERA Bob alice]
}

Definition is as follows:

func Sort[S ~[]E, E ](x S)

Sort ascending order on ordered slices. For floating point type, NaN is ahead of other values. A simple example is as follows:

package main
import (
	"fmt"
	"math"
	"slices"
)
func main() {
	s1 := []int8{0, 42, -10, 8}
	(s1) 
	(s1) // [-10 0 8 42]
	s2 := []float64{0, (), -10, 8, ()}
	(s2)
	(s2) // [NaN NaN -10 0 8]
}

Definition is as follows:

func SortFunc[S ~[]E, E any](x S, cmp func(a, b E) int)

Sorting slice x in ascending order determined by the cmp function cannot guarantee stability. The Cmp (a, b) function should return a negative number when a < b, a positive number when a > b, and zero when a == b. SortFunc requires that the cmp function is a strictly weakly sorted type. A simple example is as follows:

package main
import (
	"cmp"
	"fmt"
	"slices"
	"strings"
)
func main() {
	names := []string{"Bob", "alice", "VERA"}
	(names, func(a, b string) int {
		return ((a), (b))
	})
	(names) // [alice Bob VERA]
}

Definition is as follows:

func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int)

Sort the slice x while keeping the original order of equal elements, using cmp to compare elements in the same way as SortFunc . A simple example is as follows:

package main
import (
	"cmp"
	"fmt"
	"slices"
)
func main() {
	type Person struct {
		Name string
		Age  int
	}
	people := []Person{
		{"Gopher", 13},
		{"Alice", 20},
		{"Bob", 24},
		{"Alice", 55},
	}
	// Stable sort by name, keeping age ordering of Alices intact
	(people, func(a, b Person) int {
		return (, )
	})
	(people) // [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
}

This is the introduction to this article about the detailed explanation of the usage of slice functions in the newly added slices package in Go 1.21. For more related contents of Go 1.21 slices package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!