Golang mimics the tail command, displaying the file contents of the specified number of lines at the end of the file

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. The project needs to implement the ability to read the specified number of rows at the end of the file (similar to the tail command), and then see the implementation of the tail command. Then use the go language to follow the idea of writing a bit. The real-time output function of the log can be implemented in conjunction with Https://github.com/fsnotify/fsnotify. Similar to ' tail-f xxx.log ' effect ' package mainimport ("Bufio" "bytes" "io" "Log" "OS" "strings") func main () {lines: = Int64 ($) 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 ()% 8192data: = make ([]byte, 8192)//row of data totalbyte: = Make ([][][]byte, 0) readLine S: = Int64 (0) for I: = Int64 (0); I <= fileinfo.size ()/8192; I++{readbyte: = Make ([][]byte, 0)//read a page of data 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 & Lt to; From, to = From+1, to-1 {S[from], s[to] = s[to], S[from]}return s}func bytearraytostring (buf [][][]byte) string {str: = ma Ke ([]string, 0) for _, V: = Range Buf {For _, VV: = range v {str = append (str, string (VV))}}return strings. Join (str, "")} "if there are inappropriate places, please correct me \x00 is to remove the invalid content in the file, if the log output is normal, you can delete the relevant code 519 reads  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.