Skip to content

Archive

Search

8 articles
Go 14 Sep 2026 4 min read

Search Sorted Slices with slices.BinarySearch in Go

slices.BinarySearch returns more than a membership result. Its index identifies the earliest matching position when a target exists, and the position where that target belongs when it does not. That dual contract makes the function useful for maintaining sorted data as well as querying it. The standard-library signature accepts ordered element types: func BinarySearch[S ~[]E, E cmp.Ordered](x S, target E) (int, bool) The input must already be sorted in increasing order. The function does not sort, copy, or mutate the slice.

Go 13 Sep 2026 3 min read

Find the First Predicate Match with slices.IndexFunc in Go

slices.IndexFunc scans a slice from the beginning and returns the index of the first element accepted by a predicate. That contract is narrower than filtering: only one position is requested, and the scan has no reason to continue after a match. Its standard-library signature accepts any slice element type: func IndexFunc[S ~[]E, E any](s S, f func(E) bool) int The predicate receives each element in index order. A true result ends the search and produces that index. If every call returns false, the function returns -1.

Go 12 Sep 2026 6 min read

Search Sorted Go Slices with slices.BinarySearch

Searching a slice often starts with a loop, and for unsorted data that can be the right choice. When the slice is already sorted, slices.BinarySearch gives you a more specific operation: it finds a target without scanning every element from the beginning, and it also tells you where a missing target belongs in the current order. That second result is easy to overlook. It makes the function useful not only for membership checks, but also for maintaining sorted collections without writing separate insertion-point logic.

Go 11 Sep 2026 6 min read

Find Exact Values in Go Slices with slices.Index

A slice often holds a short sequence where you need the position of one exact value: a status in a workflow, a command-line argument, a feature name, or an ID in a small ordered list. slices.Index handles that case directly. It returns the first matching index, or -1 when the value isn’t present. That return contract is simple, but it affects how callers should use the result. Indexing the slice before checking for -1 will panic, repeated searches still scan linearly, and exact equality isn’t suitable for every data type or matching rule.

Go 11 Sep 2026 5 min read

Check Exact Values in Go Slices with slices.Contains

Code often needs a simple membership check: is this status allowed, does this short list contain a requested format, or has this identifier already appeared in a small batch? slices.Contains answers that question directly for comparable values and returns a boolean. The operation is deliberately narrow. It checks exact equality, doesn’t return a position, and scans the slice until it finds a match or reaches the end. Those details make it a good fit for some lookups and a poor fit for others.

Go 10 Sep 2026 6 min read

Search Go Struct Slices with slices.BinarySearchFunc

A slice of structs can be sorted by an ID, timestamp, or other field even though the struct itself has no built-in ordering. When you need repeated lookups in that sorted data, slices.BinarySearchFunc lets the search use the same ordering without building a separate index. The comparator is the part that deserves attention. It doesn’t compare two slice elements. It compares one element from the slice with the search target, and its ordering must agree with the way the slice is sorted.

Go 10 Sep 2026 5 min read

Find Values and Insertion Points with slices.BinarySearch

When a Go slice is already sorted, scanning it from the beginning to find one value throws away useful information. slices.BinarySearch uses that ordering directly. It returns both an index and a found flag, and the index remains useful even when the target isn’t present. That second behavior is easy to overlook. slices.BinarySearch isn’t only a membership check; it also tells you where a missing value belongs if you want to preserve the slice’s sort order.

Go 10 Sep 2026 6 min read

Check Slice Predicates in Go with slices.ContainsFunc

Sometimes you don’t need the matching element or its position. You only need to answer a yes-or-no question: does this slice contain anything that satisfies a condition? For that case, slices.ContainsFunc is more direct than writing an index loop or calling slices.IndexFunc and comparing its result with -1. It accepts a predicate, checks elements in order, and returns as soon as one matches. What slices.ContainsFunc does The function accepts any slice element type because the predicate decides what counts as a match: