Golang bytes. FieldsFunc() function example
package bytes
FieldsFunc interprets the input slice as a sequence of UTF-8-encoded Unicode code points. It splits the slice around each instance of a given rune.
Golang bytes. FieldsFunc() function usage example
package main
import (
"bytes"
"fmt"
)
func main() {
strslice := []byte("a b c d e f \tg") // \t is tab space
strfield := bytes.FieldsFunc(strslice, func(divide rune) bool {
return divide == ' ' // we divide at empty white space
})
for i, subslice := range strfield {
fmt.Printf("Counter %d : %s \n", i, string(subslice))
}
utf8slice := []byte("大世界和小世界")
utf8field := bytes.FieldsFunc(utf8slice, func(divide rune) bool {
return divide == '和'
})
for i, utf8slice := range utf8field {
fmt.Printf("Counter %d : %s \n", i, string(utf8slice))
}
}
Output :
Counter 0 : a
Counter 1 : b
Counter 2 : c
Counter 3 : d
Counter 4 : e
Counter 5 : f
Counter 6 : g
Counter 0 : 大世界
Counter 1 : 小世界
Advertisement
Something interesting
Tutorials
+21.5k Golang : How to force compile or remove object files first before rebuild?
+9.8k PHP : Get coordinates latitude/longitude from string
+10.1k CodeIgniter : Load different view for mobile devices
+23.7k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+31.3k Golang : Calculate percentage change of two values
+26.3k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+15.2k Golang : Search folders for file recursively with wildcard support
+12.6k Golang : Extract part of string with regular expression
+8.4k Golang : Find relative luminance or color brightness
+10.4k Golang : Use regular expression to get all upper case or lower case characters example
+15.9k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type