summaryrefslogtreecommitdiffstats
path: root/pkg/utils/io.go
blob: d31c5fc073e52f0b69af285fae54322f5913e1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package utils

import (
	"bufio"
	"os"
)

func ForEachLineInFile(path string, f func(string, int)) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	reader := bufio.NewReader(file)
	for i := 0; true; i++ {
		line, err := reader.ReadString('\n')
		if err != nil {
			break
		}
		f(line, i)
	}

	return nil
}