Golang bytes.Split() function example

package bytes

Split slices the given input into all subslices separated by a separator(2nd parameter) and returns a slice of the subslices between those separators. If separator parameter is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.

Golang bytes.Split() function usage example

 package main

 import (
 "fmt"
 "bytes"
 )

 func main() {

 str := []byte("I LOVE THIS WORLD!")

 for i, rune := range bytes.Split(str, []byte{' '}) { //split by white space
 fmt.Printf("Counter %d :  %s\n", i , string(rune))
 }

 }

Output :

Counter 0 : I

Counter 1 : LOVE

Counter 2 : THIS

Counter 3 : WORLD!

Reference :

http://golang.org/pkg/bytes/#Split

Advertisement