blink/v1/internal/file/read.go
2024-07-09 16:51:56 +01:00

37 lines
556 B
Go

package file
import (
"bufio"
"os"
"strings"
)
// Read a file
func Read(path string) ([]string, error) {
// Store lines
var lines []string
// Open file
file, err := os.Open(path)
if err != nil {
return lines, err
}
defer file.Close()
// Create scanner
scanner := bufio.NewScanner(file)
// Go through scanner
for scanner.Scan() {
// Store line
line := scanner.Text()
// Ignore empty & commented lines
if line != "" && !strings.HasPrefix(line, "#") {
lines = append(lines, strings.ToLower(line))
}
}
return lines, nil
}