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
+5.2k Golang : Customize scanner.Scanner to treat dash as part of identifier
+7.1k Golang : Get environment variable
+6.3k Apt-get to install and uninstall Golang
+5.4k Golang : Return multiple values from function
+6.6k Golang : Embedded or data bundling example
+13.5k Golang : How to get year, month and day?
+4.8k Golang : A program that contain another program and executes it during run-time
+5.6k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+7.4k Linux : How to fix Brother HL-1110 printing blank page problem
+6.8k Golang : Calculate pivot points for a cross
+27.2k Golang : Find files by name - cross platform example
+10.1k Golang : Identifying Golang HTTP client request