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
+22.9k Golang : Test file read write permission example
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+7.1k Restart Apache or Nginx web server without password prompt
+15.9k Golang : Get current time from the Internet time server(ntp) example
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+6.9k Golang : Fibonacci number generator examples
+20.7k Golang : Read directory content with os.Open
+7.3k Golang : How to iterate a slice without using for loop?
+7.1k Golang : A simple forex opportunities scanner
+17.8k Golang : Defer function inside init()
+6k Linux/MacOSX : Search for files by filename and extension with find command
+9.5k Mac OSX : Get a process/daemon status information