This example uses suffixarray with strings. It first converts the string literal to a byte slice. Then it calls New on the byte slice.
package main
import (
"fmt"
"index/suffixarray"
)
func main() {
// A byte string.
value := []byte(
"cat dog cat cat bird")
// Create a new suffixarray index.
index := suffixarray.New(value)
// Find all instances of this byte slice in the source slice.
cats := index.Lookup([]byte(
"cat"), -1)
fmt.Println(cats)
// Display all the substrings starting at each index found.
for i := range cats {
fmt.Println(string(value[cats[i]:]))
}
// Find just one index.
catsOne := index.Lookup([]byte(
"cat"), 1)
fmt.Println(catsOne)
// Find another index.
birds := index.Lookup([]byte(
"bird"), -1)
fmt.Println(birds)
}
[12 8 0]
cat bird
cat cat bird
cat dog cat cat bird
[12]
[16]