Skip to content

Archive

Comparison

13 articles
Go 14 Sep 2026 4 min read

Select Minimum Struct Values with slices.MinFunc in Go

slices.MinFunc selects one element from a slice according to a caller-defined ordering. Unlike sorting, it does not rearrange the input, and unlike a field-specific loop, it makes the comparison rule an explicit part of the operation. Its standard-library signature accepts any element type: func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E The comparison function returns a negative value when a precedes b, a positive value when a follows b, and zero when the two values are equal under the chosen order.

Go 14 Sep 2026 4 min read

Compact Adjacent Slice Values with slices.CompactFunc in Go

slices.CompactFunc removes repeated values only when equivalent elements are next to each other. That adjacency rule makes it different from set-style deduplication: equal values separated by another element remain separate entries. The function accepts a slice and an equality function: func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S It compacts the slice in place and returns the resulting shorter slice.

Go 13 Sep 2026 4 min read

Compare Slices with Custom Equality in Go

slices.EqualFunc compares two slices position by position while leaving the definition of equality to a supplied function. That makes the operation useful when plain == is unavailable or does not express the relation the program needs. The function also permits the two slices to have different element types: func EqualFunc[S1 ~[]E1, S2 ~[]E2, E1, E2 any]( s1 S1, s2 S2, eq func(E1, E2) bool, ) bool The result still describes sequence equality. Length, order, and every corresponding pair matter.

Go 12 Sep 2026 6 min read

Compare Go Slices with Custom Rules Using slices.CompareFunc

Two slices can represent the same ordered data even when their element types differ. One side might contain integers while another contains numeric strings, or two struct types might expose the same key through different fields. Plain slices.Compare can’t express those cases because it relies on the element type’s built-in ordering. slices.CompareFunc lets you supply the element comparison. It still compares the slices lexicographically, so the first unequal pair decides the result; the custom function only defines how each pair is ordered.

Go 11 Sep 2026 5 min read

Find the Smallest Go Slice Value with slices.Min

Finding the smallest value in a Go slice doesn’t require sorting or a hand-written comparison loop. When the element type has a natural order, slices.Min states the operation directly and leaves the input untouched. The call is compact, but its contract matters at the edges. An empty slice causes a panic, and a floating-point NaN propagates to the result. Those cases are best handled where the application can attach useful meaning to them.

Go 11 Sep 2026 5 min read

Find the Largest Go Slice Value with slices.Max

Finding the largest value in a slice is a small operation, but handwritten loops still create room for awkward initialization and missed empty-input handling. For ordered values such as integers, strings, and floating-point numbers, slices.Max expresses the operation directly. The function scans the slice and returns its maximal element. It doesn’t sort or modify the input. Two details deserve attention in production code: an empty slice causes a panic, and floating-point input containing a NaN produces a NaN result.

Go 11 Sep 2026 5 min read

Find Minimum Struct Values in Go with slices.MinFunc

Finding the smallest number in a slice is straightforward, but real programs often need the cheapest quote, earliest task, shortest route, or lowest-priority struct. Those values aren’t ordered by Go itself, so slices.Min can’t express the rule. slices.MinFunc handles that case. You provide the slice and a comparator; it returns the element that is minimal according to that comparator. What slices.MinFunc does The function accepts slices of any element type:

Go 11 Sep 2026 5 min read

Find Maximum Struct Values in Go with slices.MaxFunc

Finding the largest integer in a Go slice is easy. The problem gets more interesting when the slice contains structs and “largest” means the build with the longest duration, the newest release, or the highest application-specific score. slices.MaxFunc is designed for that case. It scans a slice and returns the maximal element according to a comparator you provide, so the ordering rule can live next to the selection instead of being hidden in a separate sort.

Go 11 Sep 2026 6 min read

Compare Struct Slices in Go with slices.CompareFunc

Two slices can represent ordered records rather than plain strings or numbers. Maybe they’re deployment steps, semantic-version parts, or structs sorted by a domain-specific key. When you need to answer which sequence comes first, slices.Compare isn’t enough if the elements don’t have Go’s built-in ordering. slices.CompareFunc handles that case. It compares two slices from left to right and lets you define what it means for one pair of elements to be less than, equal to, or greater than the other.

Go 11 Sep 2026 5 min read

Compare Go Slices for Equality with slices.Equal

Two slices can contain the same number of elements and still represent different sequences. When equality means “same length, same value at every index,” slices.Equal expresses that check directly without a manual loop. The function is small, but a few details matter in production code. Element order counts, nil and empty slices compare equal, element types must be comparable, and floating-point NaN values don’t compare equal to themselves. Compare Go slices with slices.Equal For strings, integers, booleans, and other comparable element types, pass both slices directly:

Go 11 Sep 2026 6 min read

Compare Go Maps with Custom Value Rules Using maps.EqualFunc

Two maps can represent the same application state even when their values aren’t directly comparable with ==. One map might hold structs containing slices, another might use a different value type, or a text field might be considered equal regardless of letter case. maps.EqualFunc handles that situation by keeping key comparison fixed while letting you define value equality. It is a compact option when the question is strictly whether two maps contain the same keys and equivalent values.

Go 10 Sep 2026 7 min read

Compare Go Slices with Custom Equality Using slices.EqualFunc

Two slices can represent the same information without being directly comparable element by element. One side might contain structs while the other contains strings. Text may need case-insensitive comparison. IDs may arrive in different concrete types even though your application treats them as the same value. When equality depends on a rule rather than Go’s == operator, slices.EqualFunc puts that rule in one place and handles the slice traversal for you.

Go 10 Sep 2026 6 min read

Compare Go Slices Lexicographically with slices.Compare

Sometimes equality isn’t enough. You may need to order version components, compare path segments, choose which byte sequence comes first, or sort records by a slice-valued key. Writing that comparison by hand is straightforward, but the prefix case and the meaning of the return value are easy places to introduce small bugs. For slices whose element type is ordered, slices.Compare provides the standard lexicographic comparison directly. It compares elements from left to right and, when all shared elements match, treats the shorter slice as smaller.