初级会员
  • 第 62039 位会员
  • GGXXLL
  • Trock
  • 2021-05-08 10:07:29
  • Offline
  • 20 53

最近发布的主题

    暂无

最近发布的文章

    暂无

最近发布的项目

    暂无

最近的评论

  • #3 @breadHood @buguang01 说的也对,看你在不在意那点数据了。记得调用 `defer tails.Stop()`, 如果你采用 @buguang01 说的了,记得 `ctx.Done` 下面直接调用`Stop`,然后 `for line := range tails.Lines` ,不然文件一直在写的话,其实`tails.Lines`读不完。
  • 简单改一下,不知道理解的对不对哈,始终只监听最新的文件,所以 `eventName` 每次替换 ```go go func() { var ctx,cancel = context.WithCancel(context.Background()) var eventName string for { select { case event := <-watcher.Events: // 如果是同一文件,跳过 if eventName == event.Name { continue } isMatch := re.MatchString(event.Name) if isMatch { // 更换文件了,cancel 上次的 go DealLog cancel() // 文件名更新 eventName = event.Name if event.Op&fsnotify.Create == fsnotify.Create { // 监控创建文件动作 fmt.Println(event.Name) // 重新赋值 ctx, cancel = context.WithCancel(context.Background()) go DealLog(ctx, event.Name) } if event.Op&fsnotify.Write == fsnotify.Write { // 监控写入文件动作 fmt.Println(event.Name) ctx, cancel = context.WithCancel(context.Background()) go DealLog(ctx, event.Name) } } case err := <-watcher.Errors: log.Println(err) } time.Sleep(time.Second * 1) } }() ``` ```go // 读取每行数据,最后写入到AllAddrLoglist中. for { select { case line, ok = <-tails.Lines: .... case <-ctx.Done(): return } } ```