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
+9.5k Golang : Extract or copy items from map based on value
+26.9k Golang : Force your program to run with root permissions
+20.7k Golang : Read directory content with os.Open
+5.7k Golang : Error handling methods
+10.4k Golang : cannot assign type int to value (type uint8) in range error
+15.7k Golang : Get checkbox or extract multipart form data value example
+13.2k Golang : Convert(cast) int to int64
+7.8k Golang : Example of how to detect which type of script a word belongs to
+10.3k Golang : Embed secret text string into binary(executable) file
+10k Golang : Channels and buffered channels examples
+20.5k nginx: [emerg] unknown directive "passenger_enabled"