Golang bytes.Fields() function example

package bytes

Fields splits the input slice around each instance of one or more consecutive white space characters.

Golang bytes.Fields() function usage example

 package main

 import (
 "bytes"
 "fmt"
 )

 func main() {

 strslice := []byte("a b c d e f \tg") // \t is tab space

 for i, subslice := range bytes.Fields(strslice) {
 fmt.Printf("Counter %d : %s \n", i, string(subslice))
 }

 }

Output :

Counter 0 : a

Counter 1 : b

Counter 2 : c

Counter 3 : d

Counter 4 : e

Counter 5 : f

Counter 6 : g

Advertisement