MCPcopy
hub / github.com/gin-gonic/gin / readNthLine

Function readNthLine

recovery.go:146–169  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

144// or an empty string if the line doesn't exist.
145// If there's an error opening the file, it returns the error.
146func 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.
172func function(pc uintptr) string {

Callers 2

TestReadNthLineFunction · 0.85
stackFunction · 0.85

Calls 2

CloseMethod · 0.80
OpenMethod · 0.45

Tested by 1

TestReadNthLineFunction · 0.68