How to split a slice of bytes in Go

3 Answers

0 votes
package main
 
import (
    "fmt"
    "bytes"
)
 
func main() {
    slice := []byte{'G', 'O', ' ' , '1', '.', 'a', '1', '4', '.', '2'} 

    slicesplit := bytes.Split(slice, []byte("1")) 
     
    fmt.Printf("%s", slicesplit)    
}
 
 
 
/*
run:
 
[GO  .a 4.2]
 
*/

 



answered May 25, 2020 by avibootz
0 votes
package main
 
import (
    "fmt"
    "bytes"
)
 
func main() {
    slicesplit :=  bytes.Split([]byte("ab,cd,ef"),[]byte(",")) 
     
    fmt.Printf("%s", slicesplit)    
}
 
 
 
/*
run:
 
[ab cd ef]
 
*/

 



answered May 25, 2020 by avibootz
0 votes
package main
 
import (
    "fmt"
    "bytes"
)
 
func main() {
    slicesplit :=  bytes.Split([]byte("abccdbdbefbbxyz"),[]byte("b")) 
     
    fmt.Printf("%s", slicesplit)    
}
 
 
 
/*
run:
 
[a ccd d ef  xyz]
 
*/

 



answered May 25, 2020 by avibootz

Related questions

1 answer 230 views
2 answers 206 views
2 answers 183 views
2 answers 180 views
2 answers 225 views
2 answers 167 views
...