项目中需要实现读取文件末尾指定行数的功能(类似于tail命令),就去看了一下tail命令的实现思路。然后用go语言按照思路仿写了一下。后面可以结合 https://github.com/fsnotify/fsnotify 实现日志的实时输出功能。类似于 `tail -f xxx.log`效果
```
package main
import (
"bufio"
"bytes"
"io"
"log"
"os"
"strings"
)
func main(){
lines := int64(200)
file, err := os.Open("C:\\Users\\Administrator\\Desktop\\test.log")
if err != nil {
log.Println(err)
return
}
fileInfo, _ := file.Stat()
buf := bufio.NewReader(file)
offset := fileInfo.Size() % 8192
data := make([]byte, 8192) // 一行的数据
totalByte := make([][][]byte, 0)
readLines := int64(0)
for i := int64(0); i <= fileInfo.Size() / 8192; i++{
readByte := make([][]byte, 0) // 读取一页的数据
file.Seek(fileInfo.Size() - offset - 8192*i, io.SeekStart)
data = make([]byte, 8192)
n, err := buf.Read(data)
if err == io.EOF {
if strings.TrimSpace(string(bytes.Trim(data, "\x00"))) != ""{
readLines++
readByte = append(readByte, data)
totalByte = append(totalByte, readByte)
}
if readLines > lines{
break
}
continue
}
if err != nil {
log.Println("Read file error:", err)
return
}
strs := strings.Split(string(data[:n]), "\n")
if len(strs) == 1{
b := bytes.Trim([]byte(strs[0]), "\x00")
if len(b) == 0{
continue
}
}
if (readLines + int64(len(strs))) > lines{
strs = strs[int64(len(strs))-lines+readLines:]
}
for j:=0;j<len(strs);j++{
readByte = append(readByte, bytes.Trim([]byte(strs[j]+"\n"),"\x00"))
}
readByte[len(readByte)-1] = bytes.TrimSuffix(readByte[len(readByte)-1], []byte("\n"))
totalByte = append(totalByte, readByte)
readLines += int64(len(strs))
if readLines >= lines{
break
}
}
totalByte = ReverseByteArray(totalByte)
log.Println(ByteArrayToString(totalByte))
}
func ReverseByteArray(s [][][]byte) [][][]byte {
for from, to := 0, len(s)-1; from < to; from, to = from+1, to-1 {
s[from], s[to] = s[to], s[from]
}
return s
}
func ByteArrayToString(buf [][][]byte) string {
str := make([]string, 0)
for _, v := range buf {
for _, vv := range v {
str = append(str, string(vv))
}
}
return strings.Join(str, "")
}
```
如果有不当的地方,欢迎指正
\x00是为了去除文件中无效的内容,如果日志输出内容正常,可以删掉相关代码
有疑问加站长微信联系(非本文作者))