I currently have a problem in my project where i must find numbers that are not present in a slice. I found working code in the golang website, this works great. In my project i make a empty block and let the else statement do all the work. I tried to alter the code to remove the empty block but i got errors every time, i finaly found a example that reproduces the problem :
package main
import (
"fmt"
"sort"
)
func main() {
data := []int{27, 15, 8, 9, 12, 4, 17, 19, 21, 23, 25}
nr := 9
sort.Ints(data)
index := sort.Search(len(data), func(index int) bool { return data[index] == nr })
if index == len(data) {
fmt.Print("It's not in : ")
fmt.Println(nr)
} else {
fmt.Print("It's in! Index is at : ")
fmt.Println(index)
}
}
Working code on golang playground!