Golang bufio.ScanBytes() function example

package bufio

ScanBytes is a split function for a Scanner that returns each byte as a token.

bufio.ScanBytes() function usage example

 file, err := os.Open("for")

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

 defer file.Close()

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

 scanner.Split(bufio.ScanBytes)

 for scanner.Scan() {
 fmt.Println(scanner.Bytes())
 }

Advertisement