Skip to content

hlts2/gticker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gticker

gticker is time ticker with immediate first tick.

The Go standard library has a really cool type – Ticker. Tickers are used when you want to do something at a regular interval. However, if we want to tic immediately the first time, we can cut it out to a function and call it before tic. This package was created for the purpose of being able to handle a different approach from that implementation.

Requirement

Go 1.16

Installation

go get github.com/hlts2/gticker

Example

package main

import (
    "fmt"
    "time"

    "github.com/hlts2/gticker"
)

func main() {
    ticker := gticker.NewInstantTicker(1 * time.Second)
    done := make(chan bool)

    go func() {
        for {
            select {
            case <-done:
                return
            case t := <-ticker.C():
                fmt.Println("Tick at", t)
            }
        }
    }()

    time.Sleep(3 * time.Second)
    ticker.Stop()
    done <- true
    fmt.Println("Ticker stopped")
}

Author

hlts2

About

Time ticker with immediate first tick

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages