Golang bufio.ScanLines() function example

package bufio

ScanLines is a split function for a Scanner that returns each line of text, stripped of any trailing end-of-line marker. The returned line may be empty. The end-of-line marker is one optional carriage return followed by one mandatory newline. In regular expression notation, it is \r?\n. The last non-empty line of input will be returned even if it has no newline.

Golang bufio.ScanLines() function usage example

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

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

 defer file.Close()

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

 scanner.Split(bufio.ScanLines)

 for scanner.Scan() {

 fmt.Println(scanner.Text())

 }

Advertisement