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 :
Advertisement
Something interesting
Tutorials
+6.8k Golang : Calculate pivot points for a cross
+9.2k Golang : Create and shuffle deck of cards example
+21.6k Golang : GORM create record or insert new record into database example
+12k Golang : Decompress zlib file example
+6.1k Golang : Dealing with backquote
+43.5k Golang : Get hardware information such as disk, memory and CPU usage
+5.7k Fix yum-complete-transaction error
+13.6k Golang : Strings comparison
+10.5k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6k Golang : How to verify input is rune?
+28k Golang : Move file to another directory
+21.2k Golang : Clean up null characters from input data