Golang bytes.Index() function example

package bytes

Index returns the index of the first instance of the search key in the search input. The index function returns -1 if the search key is not present in search input.

Golang bytes.Index() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

 search_input := []byte("abcd和efghi")

 fmt.Println(bytes.Index(search_input, []byte("和")))

 fmt.Println(bytes.Index(search_input, []byte("123"))) // will return -1

 fmt.Println(bytes.Index(search_input, []byte("abcd")))
 }

Output :

4 <------ index start counting from 0 ! therefore 和 is at index number 4

-1

0

Reference :

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

Advertisement