parseFile reads a file given its filename and returns a list containing each of its lines.
(filename string)
| 79 | |
| 80 | // parseFile reads a file given its filename and returns a list containing each of its lines. |
| 81 | func parseFile(filename string) ([]string, error) { |
| 82 | file, err := os.Open(filename) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | defer func(file *os.File) { |
| 87 | if err := file.Close(); err != nil { |
| 88 | log.Printf("Error closing file: %v", err) |
| 89 | } |
| 90 | }(file) |
| 91 | |
| 92 | var lines []string |
| 93 | scanner := bufio.NewScanner(file) |
| 94 | for scanner.Scan() { |
| 95 | line := strings.TrimRight(scanner.Text(), "\r") |
| 96 | if line == "" { |
| 97 | continue |
| 98 | } |
| 99 | lines = append(lines, line) |
| 100 | } |
| 101 | if err := scanner.Err(); err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | return lines, nil |
| 106 | } |
| 107 | |
| 108 | // header represents an HTTP header. |
| 109 | type header struct { |
no outgoing calls