Golang bufio.ScanRunes() function example

package bufio

ScanRunes is a split function for a Scanner that returns each UTF-8-encoded rune as a token. The sequence of runes returned is equivalent to that from a range loop over the input as a string, which means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd". Because of the Scan interface, this makes it impossible for the client to distinguish correctly encoded replacement runes from encoding errors.

bufio.ScanRunes() function usage example

 file, err := os.Open("utf8.txt")

 if err != nil {
 panic(err.Error())
 }

 defer file.Close()

 reader := bufio.NewReader(file)
 scanner := bufio.NewScanner(reader)

 scanner.Split(bufio.ScanRunes)

 for scanner.Scan() {

 fmt.Printf("%s ", scanner.Text())

 }

Advertisement