Go

Go

Made by DeepSource

time.Tick used in leaky way SCC-SA1015

Performance
Critical

Using time.Tick in a way that will leak. Consider using time.NewTicker, and only use time.Tick in tests, commands and endless functions.

Bad practice

import (
    "fmt"
    "time"
)

func foo() {
    c := time.Tick(5 * time.Second)

    for next := range c {
        fmt.Println(next)
        // No way to cancel or reset the timer
    }
}

Recommended

import (
    "fmt"
    "time"
)

func foo() {
    ticker := time.NewTicker(5 * time.Second)

    go func() {
        time.Sleep(20 * time.Second)
        ticker.Stop()
    }()

    for next: = range ticker.C {
        fmt.Println(next)
        // Will only go until the first 20 seconds
    }
}