-3

How can I use fmt.Println only every 5 min

h := fmt.Sprintf("%x", word)
fmt.Println(h)

I need something like this: if time := 5 min then fmt.Println(h)

just to show (n) only every 5 min not every line

EDIT: Obviously I haven't asked the correct question.

There is a cycle which generate random digits every second.

for i := n1; i.Cmp(n2) < 0; i.Add(i, one) {

What I want is: not to show every generated variable (i) but just one in every 5 minutes.

Mad
  • 101
  • 1
  • 6

3 Answers3

2

If do something after 5min, simply use time.AfterFunc:

time.AfterFunc(time.Minute * 5, func() {
    // Do something after 5min
    h := fmt.Sprintf("%x", word)
    fmt.Println(h)
})

Else if do something every 5min, use for loop (and go routine if need):

for{
    time.Sleep(time.Minute * 5)
    // Do something every 5min
    h := fmt.Sprintf("%x", word)
    fmt.Println(h)
}

Other way, use time.NewTicker can get a time counter:

ticker:=time.NewTicker(time.Minute * 5)
for{
    select{
    case <-ticker.C:
        // Do something every 5min
        h := fmt.Sprintf("%x", word)
        fmt.Println(h)
    }
}
xu feng
  • 119
  • 7
1

If you want to do something only once after 5 minutes you can use goroutine and WaitGroup:

var wg sync.WaitGroup
wg.Add(1)

go func() {
   defer wg.Done()
   time.Sleep(...)
   fmt.Println("hello")
}()
wg.Wait()

Go playground example: https://play.golang.org/p/UxJ76f44k3x

If you want to do something every 5 minutes:

func doEvery(d time.Duration, f func(time.Time)) {
    for x := range time.Tick(d) {
        f(x)
    }
}

func helloworld(t time.Time) {
    fmt.Printf("%v: Hello, World!\n", t)
}

func main() {
    doEvery(3*time.Second, helloworld)
}

https://play.golang.org/p/0S3MfGlgVRL

ceth
  • 44,198
  • 62
  • 180
  • 289
1

There are ways to do a task repeatedly. It is up to you which one you choose. Some are listed:

  • Simplest way (may be):

    go func() {
        for {
            fmt.Println("hello")
            time.Sleep(5 * time.Minute)
        }
    }()
    time.Sleep(30 * time.Minute)
    
  • func NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument.

    ticker := time.NewTicker(5 * time.Minute)
    defer ticker.Stop()
    done := make(chan bool)
    go func() {
        time.Sleep(30 * time.Minute)
        done <- true
    }()
    for {
        select {
        case <-done:
            fmt.Println("Done!")
            return
        case <-ticker.C:
            fmt.Println("hello")
        }
    }
    

    https://play.golang.org/p/_Deey4oJDNn

  • If you don't like to use the channel, then

    go func() {
        for range time.Tick(5 * time.Second){
            fmt.Println("hello")
        }
    }()
    time.Sleep(30 * time.Second)
    

    https://play.golang.org/p/s2efNToSymM

  • https://github.com/robfig/cron is also helpful.

    c := cron.New()
    c.AddFunc("@every 5m", func() { fmt.Println("hello") })
    c.Start()
    select {}
    

UPDATE

Shudipta Sharma
  • 5,178
  • 3
  • 19
  • 33