github.com/blend/go-sdk@v1.20220411.3/fileutil/read_lines.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package fileutil 9 10 import ( 11 "bufio" 12 "os" 13 14 "github.com/blend/go-sdk/ex" 15 ) 16 17 // ReadLines reads a file and calls the handler for each line. 18 func ReadLines(filePath string, handler func(string) error) error { 19 f, err := os.Open(filePath) 20 if err != nil { 21 return ex.New(err) 22 } 23 defer f.Close() 24 25 scanner := bufio.NewScanner(f) 26 for scanner.Scan() { 27 line := scanner.Text() 28 err = handler(line) 29 if err != nil { 30 return ex.New(err) 31 } 32 } 33 return nil 34 }