.e.g.
1. logfile
- Start
- Line1
- Line2
- Line3
- End
I am able to get the seek position of Line1 when I read the file from beginning.
func getSeekLocation() int64 {
start := int64(0)
input, err := os.Open(logFile)
if err != nil {
fmt.Println(err)
}
if _, err := input.Seek(start, io.SeekStart); err != nil {
fmt.Println(err)
}
scanner := bufio.NewScanner(input)
pos := start
scanLines := func(data []byte, atEOF bool) (advance int, token []byte,
err error) {
advance, token, err = bufio.ScanLines(data, atEOF)
pos += int64(advance)
return
}
scanner.Split(scanLines)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "Line1") {
break
}
}
size, err := getFileSize()
if err != nil {
fmt.Println(err)
}
return size - pos
}
But this is not an efficient way to solve the problem because as the file size increases the time to get the location will also increase.
I would like to get the location of the line from the EOF location which I think would be more efficient.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…