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 190 views
1 answer 260 views
2 answers 274 views
2 answers 215 views
2 answers 207 views
2 answers 259 views
...