package main
import (
"fmt"
"sort"
)
func main() {
// integer slice in unsort order
intSlice := []int{10, 3, 6, 10, 15, 21, 38, 26, 25, 45}
a := 15
pos := sort.SearchInts(intSlice, a)
fmt.Printf("Found %d at index %d in %v\n", a, pos, intSlice)
// string slice in unsorted order
strSlice := []string{"Pink", "Orange",
"Green", "Black",
"Purple", "Blue", "Red"}
b := "Green"
pos = sort.SearchStrings(strSlice, b)
fmt.Printf("Found %s at index %d in %v\n",
b, pos, strSlice)
// string slice in unsorted order
fltSlice := []float64{612.15, 114.510,
211.144, 396.242, 485.143}
c := 211.144
pos = sort.SearchFloat64s(fltSlice, c)
fmt.Printf("Found %f at index %d in %v\n",
c, pos, fltSlice)
// sorted slice in descending
d := []int{45, 38, 26, 25, 21, 15, 10, 10, 6, 3}
e := 38
i := sort.Search(len(d), func(i int) bool { return d[i] <= e })
if i < len(d) && d[i] == e {
fmt.Printf("found %d at index %d in %v\n", e, i, d)
} else {
fmt.Printf("%d not found in %v\n", e, d)
}
}