drg14799 2019-07-11 20:05
浏览 61
已采纳

去郎,不明白这段代码做什么

I am noob in golang, but I would like to change a source code that writes data into database every minute to every second. I have trobles to find what Tick does in the code. The config.SampleRate is integer = 1, which means every minute = every 60 seconds

What this tick is all about and the end part of it: <-tick, combined with counter i?

i := 0

tick := time.Tick(time.Duration(1000/config.Samplerate) * time.Millisecond)

for {
    // Restart the accumulator loop every 60 seconds.
    if i > (60*config.Samplerate - 1) {
        i = 0
        //some code here
    }

    //some code there

    }
    <-tick
    i++
  • 写回答

1条回答 默认 最新

  • doubairan4213 2019-07-11 20:47
    关注

    tick is a channel in Go. If you look at the docs, tick should send something to the channel once each time interval, which is specified by time.Duration(1000/config.Samplerate) * time.Millisecond in your code. <-tick just waits for that time interval to pass.

    i keeps track of how many seconds pass, so every time it ticks, you add one to i. The if statement checks when one minute passes.

    So, the code inside the if statement fires every 60 seconds, while the code right under the if block fires every second.

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

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?