sort.Search
package main
import (
"fmt"
"sort"
)
func GuessingGame() {
var s string
fmt.Println("Pick an integer from 0 to 100.")
answer := sort.Search(100, func(i int) bool {
fmt.Printf("Is your number <= %d? ", i)
_, _ = fmt.Scanf("%s", &s)
return s != "" && s[0] == 'y'
})
fmt.Printf("Your number is %d.\n", answer)
}
func main() {
s := []int{1, 2, 3, 4, 5, 6}
pos := sort.Search(len(s), func(i int) bool {
return s[i] >= 3
})
fmt.Println(pos)
GuessingGame()
}