Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
binary search in go
package main
import (
"fmt"
"sort"
)
type Thingy struct {
Woop int
}
func main() {
data := []Thingy{
{1},
{2},
{3},
{23},
{50},
}
x := Thingy{23}
i := sort.Search(len(data), func(i int) bool { return data[i].Woop >= x.Woop })
if i < len(data) && data[i] == x {
fmt.Printf("found at: %d\n", i)
} else {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment