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 237 views
2 answers 241 views
2 answers 231 views
1 answer 273 views
1 answer 232 views
1 answer 272 views
1 answer 213 views
213 views asked Sep 15, 2020 by avibootz
...