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) EParameters & Attributes
| Name | Type | Description |
|---|---|---|
| x | S | Input value or collection to process. |
| cmp | func(a, b E) int | Comparison 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)) }))}