readNthLine reads the nth line from the file. It returns the trimmed content of the line if found, or an empty string if the line doesn't exist. If there's an error opening the file, it returns the error.
(file string, n int)
| 144 | // or an empty string if the line doesn't exist. |
| 145 | // If there's an error opening the file, it returns the error. |
| 146 | func readNthLine(file string, n int) (string, error) { |
| 147 | if n < 0 { |
| 148 | return "", nil |
| 149 | } |
| 150 | |
| 151 | f, err := os.Open(file) |
| 152 | if err != nil { |
| 153 | return "", err |
| 154 | } |
| 155 | defer f.Close() |
| 156 | |
| 157 | scanner := bufio.NewScanner(f) |
| 158 | for i := 0; i < n; i++ { |
| 159 | if !scanner.Scan() { |
| 160 | return "", nil |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if scanner.Scan() { |
| 165 | return strings.TrimSpace(scanner.Text()), nil |
| 166 | } |
| 167 | |
| 168 | return "", nil |
| 169 | } |
| 170 | |
| 171 | // function returns, if possible, the name of the function containing the PC. |
| 172 | func function(pc uintptr) string { |