已经有两个月没有写博客了,也有好几个月没有看go相关的内容了,由于工作原因最近在做java以及大数据相关的内容,导致最近工作较忙,博客停止了更新,正好想捡起之前go的东西,所以找了一个源码学习
这个也是之前用go写日志收集的时候用到的一个包 :github.com/hpcloud/tail, 这次就学习一下人家的源码,为了方便看这个代码,我将这个包进行了简化,也是用于方便理解,代码放到了:https://github.com/pythonsite/tail, 这个代码包可能无法正常用,只是为了方面理解tail这个包,以及学习人家的代码
精简后的代码目录
│ tail.go │ └─watch filechanges.go inotify.go inotify_tracker.go watch.go
tail.go: 这里包含着tail包的核心代码,主要的逻辑处理时在这个里面
watch: 这个包主要用于对文件的监控,用于将文件的变化通知到tail.如:文件修改了,文件删除了,文件内容追加了
tail.go 代码分析
在tail.go中主要有几下几个结构体:
// Line 结构体用于存读每行的时候的对象type Line struct { Text string //当前行的内容 Time time.Time // 时间 Err error // Error from tail} type SeekInfo struct { Offset int64 Whence int } // 关于配置的结构体type Config struct { Location *SeekInfo ReOpen bool MustExist bool // 要打开的文件是否必须存在 Poll bool Pipe bool Follow bool // 是否继续读取新的一行,可以理解为tail -f 命令 } // 核心的结构体Tailtype Tail struct { Filename string // 要打开的文件名 Lines chan *Line // 用于存每行内容的Line结构体 Config watcher watch.FileWatcher changes *watch.FileChanges tomb.Tomb file *os.File reader *bufio.Reader lk sync.Mutex }
Line 结构体用于存读取文件的每行内容
Tail 是核心的结构体,我们使用tail这个包的时候其实就是会先调用初始化这个struct的方法TailFile,如我在写日志收集的时候的使用:
tail,err := tail.TailFile(conf.LogPath,tail.Config{ ReOpen:true, Follow:true, Location:&tail.SeekInfo{Offset:0,Whence:2}, MustExist:false, Poll:true, })

