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.

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

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景