duanlei2150 2019-02-02 14:15
浏览 46
已采纳

转到lang多等待组,计时器最后停止

i have written the following code in order to run until someone exit the program manually.

it does is

  • ----- check if the exists every 1 second

  • ----- if available then read the file and print the file content line by line

for this i have first call a function from the main and then i call a waitgroup and call a function again from there to do the aforementioned tasks.

please check if i have written the source code correctly as im a newbi on GO

plus this only runs once and stop... i want to it keep alive and see if the file exsists

please help me

 package main

import (
    "encoding/csv"
    "fmt"
    "io"
    "log"
    "os"
    "sync"
    "time"
)

func main() {
    mainfunction()
}

//------------------------------------------------------------------

func mainfunction() {
    var wg sync.WaitGroup
    wg.Add(1)

    go filecheck(&wg)

    wg.Wait()
    fmt.Printf("Program finished 
")

}

func filecheck(wg *sync.WaitGroup) {

    for range time.Tick(time.Second * 1) {
        fmt.Println("Foo")

        var wgi sync.WaitGroup
        wgi.Add(1)

        oldName := "test.csv"
        newName := "testi.csv"

        if _, err := os.Stat(oldName); os.IsNotExist(err) {
            fmt.Printf("Path does not exsist 
")
        } else {
            os.Rename(oldName, newName)
            if err != nil {
                log.Fatal(err)
            }
            looping(newName, &wgi)
        }
        fmt.Printf("Test complete 
")
        wgi.Wait()
        wg.Done()
        time.Sleep(time.Second * 5)
    }
}

func looping(newName string, wgi *sync.WaitGroup) {
    file, _ := os.Open(newName)
    r := csv.NewReader(file)
    for {
        record, err := r.Read()
        if err == io.EOF {
            break
        }
        if err != nil {
            log.Fatal(err)
        }

        var Date = record[0]
        var Agent = record[1]
        var Srcip = record[2]
        var Level = record[3]

        fmt.Printf("Data: %s Agent: %s Srcip: %s Level: %s
", Date, Agent, Srcip, Level)
    }
    fmt.Printf("Test complete 2 
")
    wgi.Done()

    fmt.Printf("for ended")
}
  • 写回答

1条回答 默认 最新

  • duan0427 2019-02-02 15:40
    关注

    The short answer is that you have this in the loop:

        wg.Done()
    

    Which makes the main goroutine proceed to exit as soon as the file is read once.


    The longer answer is that you're not using wait groups correctly here, IMHO. For example there's absolutely no point in passing a WaitGroup into looping.

    It's not clear what your code is trying to accomplish - you certainly don't need any goroutines to just perform the task you've specified - it can all be gone with no concurrency and thus simpler code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路