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 228 views
2 answers 217 views
2 answers 202 views
1 answer 264 views
1 answer 224 views
1 answer 257 views
1 answer 200 views
200 views asked Sep 15, 2020 by avibootz
...