time.Tick() Function in Golang With Examples
Last Updated :
21 Apr, 2020
In Go language,
time packages supplies functionality for determining as well as viewing time. The
Tick() function in Go language is a utility wrapper for
NewTicker function. It only allows access to the ticking channel. In addition,
Tick is beneficial for clients who don't require to shut down the Ticker. And Tick method returns nil if the duration stated is less than or equal to 0. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functions.
Syntax:
func Tick(d Duration) <-chan Time
Here,
d is the duration of time for ticker to tick, and
chan is the ticking channel.
Note: Tickers are used to do something frequently at regular intervals of the stated time.
Return Value: It returns current date and actual time after regular interval of time. And returns nil if d <= 0.
Example 1:
C
// Golang program to illustrate the usage of
// Tick() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Defining UTCtime
func UTCtime() string {
return ""
}
// Main function
func main() {
// Calling Tick method
// using range keyword
for tick := range time.Tick(3 * time.Second) {
// Prints UTC time and date
fmt.Println(tick, UTCtime())
}
}
Output:
2020-04-02 03:16:22.27131713 +0000 UTC m=+3.000249815
2020-04-02 03:16:25.271334601 +0000 UTC m=+6.000267330
2020-04-02 03:16:28.271312516 +0000 UTC m=+9.000245191
2020-04-02 03:16:31.271369788 +0000 UTC m=+12.000302595
2020-04-02 03:16:34.271309254 +0000 UTC m=+15.000241952
2020-04-02 03:16:37.271324182 +0000 UTC m=+18.000256858
2020-04-02 03:16:40.271322789 +0000 UTC m=+21.000255504
2020-04-02 03:16:43.271295568 +0000 UTC m=+24.000228305......so on
Here, we have used the range keyword in order to iterate over the channel. Here, the current date and time are returned after a regular interval of time. So, the output is different at different runs. And here the loop doesn't stops until you terminate it.
Example 2:
C
// Golang program to illustrate the usage of
// Tick() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Defining UTCtime
func UTCtime() string {
return ""
}
// Main function
func main() {
// Calling Tick method using range
// keyword
for tick := range time.Tick(-1 * time.Second) {
// Prints UTC time and date
fmt.Println(tick, UTCtime())
}
}
Output:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive (nil chan)]:
main.main()
/home/runner/SociableInsubstantialCommunication/main.go:23 +0x149
Here, in the above code, the duration stated is negative so, nil is returned and the error occurs.
Similar Reads
time.NewTicker() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The NewTicker() function in Go language is used to output a new Ticker that contains a channel in order to transmit the time with a period as stated by the duration parameter. It is helpful in setting the i
2 min read
time.Since() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Since() function in Go language holds time value and is used to evaluate the difference with the actual time. Moreover, this function is defined under the time package. Here, you need to import the "tim
2 min read
time.String() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The String() function in Go language is used to find a string that represents the duration formats as "24h6m0.7s". Here, the foremost zero units are deleted. And the stated duration with less than one-secon
2 min read
time.Ticker.Stop() Function in Golang With Examples In Go language, time packages supply functionality for determining as well as viewing time. The Stop() function in Go language is used to disable a ticker. So, after calling Stop() method no further ticks will be transmitted. And it doesn't close the channel, in order to avoid a concurrent go-routin
2 min read
time.Now() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Now() function in Go language is used to find the current local time. Moreover, this function is defined under the time package. Here, you need to import the "time" package in order to use these functio
2 min read