How to find the index of specific sequence in byte slice with Go

2 Answers

0 votes
package main

import (
    "bytes"
    "fmt"
)

func main() {
    slice := []byte("go java c++")
    
    i := bytes.Index(slice, []byte("java"))
    
    fmt.Println(i)
}




/*
run:

3

*/

 



answered Sep 15, 2020 by avibootz
0 votes
package main

import (
    "bytes"
    "fmt"
)

func main() {
    slice := []byte("go java c++")
    
    i := bytes.Index(slice, []byte("php"))
    
    fmt.Println(i)
}




/*
run:

-1

*/

 



answered Sep 15, 2020 by avibootz

Related questions

2 answers 229 views
2 answers 223 views
2 answers 202 views
1 answer 264 views
1 answer 226 views
1 answer 261 views
1 answer 201 views
201 views asked Sep 15, 2020 by avibootz
...