How to find the first index of a value in slice of bytes in Go

2 Answers

0 votes
package main
 
import (
    "fmt"
    "bytes"
)
 
func main() {
    slice := []byte{'G', 'O', ' ' , '1', '.', 'a', '1', '4', '.', 'a', '2'} 
    
    index := bytes.Index(slice, []byte("a")) 
     
    fmt.Printf("%d", index)    
}
 
 
 
/*
run:
 
5
 
*/

 



answered May 25, 2020 by avibootz
0 votes
package main
  
import (
    "fmt"
    "bytes"
)
  
func main() {
    index := bytes.Index([]byte("axbccdbdbefbbxyz"),[]byte("b")) 
      
    fmt.Printf("%d", index)    
}
  
  
  
/*
run:
  
2
  
*/

 



answered May 25, 2020 by avibootz

Related questions

2 answers 202 views
1 answer 264 views
2 answers 288 views
2 answers 225 views
2 answers 214 views
2 answers 265 views
...