Stream read file in Golang
func readFile(sourceFile string) (<-chan string, error) {
file, err := os.Open(a.sourceFile)
if err != nil {
return nil, fmt.Errorf("can't open file: %w", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("can't read file: %w", err)
}
out := make(chan string)
go func() {
defer close(out)
for scanner.Scan() {
out <- scanner.Text()
}
}()
return out, nil
}