官方地址:https://github.com/hpcloud/tail

    功能:实现类似于Linux中tail -f功能

    测试:

    1. package main
    2. import (
    3. "fmt"
    4. "github.com/hpcloud/tail"
    5. "time"
    6. )
    7. func main() {
    8. filename := "./app.log"
    9. tails, err := tail.TailFile(filename, tail.Config{
    10. ReOpen: true,
    11. Follow: true,
    12. // Location: &tail.SeekInfo{Offset: 0, Whence: 2},
    13. MustExist: false,
    14. Poll: true,
    15. })
    16. if err != nil {
    17. fmt.Println("tail file err:", err)
    18. return
    19. }
    20. var msg *tail.Line
    21. var ok bool
    22. for {
    23. msg, ok = <-tails.Lines
    24. if !ok {
    25. fmt.Printf("tail file close reopen, filename:%s\n", tails.Filename)
    26. time.Sleep(100 * time.Millisecond)
    27. continue
    28. }
    29. fmt.Printf("msg(%T): %v\n", msg,msg.Text)
    30. }
    31. }
    1. go mod init
    2. go build