Golang bytes.IndexFunc() function example

package bytes

IndexFunc interprets given input as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the search key in the given input of the first Unicode code point satisfying the search and return -1 if there is no match.

Golang bytes.IndexFunc() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {
 utf8slice := []byte("大世界和小世界")

 utf8byteindex := bytes.IndexFunc(utf8slice, func(searchkey rune) bool {
 return searchkey == '和'
 })

 fmt.Printf("和 index num is %d inside the string %s\n",  utf8byteindex, string(utf8slice))

 }

Output :

和 index num is 9 inside the string 大世界和小世界

Reference :

http://golang.org/pkg/bytes/#IndexFunc

Advertisement