Engineer in Tokyo

TIL: Go's sort.Find function

Today I learned about the sort.Find function in the Go standard library. It performs a binary search over a sorted array when provided with a comparator function.

sorted := make([]string, 150)
slices.SortFunc(sorted, strings.Compare)

// Search for target in sorted from [0,n)
target := "some query"
n := len(sorted)
if i, found := sort.Find(n, func(i int) int {
    return strings.Compare(target, sorted[i])
}); found {
    fmt.Println("value found at index %d", i)
}

This worked well for a searching simple dictionary index in the go-stardict library I’ve been working on.