Standard library / slices

slices.MaxFunc()

Returns the first maximal element under the comparator; panics on empty input. Expected stdout: 'bbb\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
xSInput value or collection to process.
cmpfunc(a, b E) intComparison callback; negative, zero, or positive for less, equivalent, or greater.

Return Value

E

Returns the first maximal element under the comparator; panics on empty input.

Working Example

Example Snippetgo
package main
import (  "cmp"  "fmt"  "slices")
func main() {  fmt.Println(slices.MaxFunc([]string{"a", "bbb", "cc"}, func(a, b string) int { return cmp.Compare(len(a), len(b)) }))}
Go Reference