time.AfterFunc() 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
AfterFunc() function in Go language is used to wait for the duration of time to pass and after that, it calls the defined function "f" in its own go-routine. 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 AfterFunc(d Duration, f func()) *Timer
Here, *Timer is a pointer to the Timer.
Return Value: It returns a
Timer which is then used to cancel the call with the help of its Stop() method.
Example 1:
C
// Golang program to illustrate the usage of
// AfterFunc() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Main function
func main() {
// Defining duration parameter of
// AfterFunc() method
DurationOfTime := time.Duration(3) * time.Second
// Defining function parameter of
// AfterFunc() method
f := func() {
// Printed when its called by the
// AfterFunc() method in the time
// stated above
fmt.Println("Function called by "+
"AfterFunc() after 3 seconds")
}
// Calling AfterFunc() method with its
// parameter
Timer1 := time.AfterFunc(DurationOfTime, f)
// Calling stop method
// w.r.to Timer1
defer Timer1.Stop()
// Calling sleep method
time.Sleep(10 * time.Second)
}
Output:
Function called by AfterFunc() after 3 seconds
Here, the output is returned after 3 seconds and then the returned timer cancels the call to the function using Stop() method. After then the program is exited after the duration of sleep ends.
Example 2:
C
// Golang program to illustrate the usage of
// AfterFunc() function
// Including main package
package main
// Importing fmt and time
import (
"fmt"
"time"
)
// Main function
func main() {
// Creating channel using
// make keyword
mychan := make(chan int)
// Calling AfterFunc() method
// with its parameters
time.AfterFunc(6*time.Second, func() {
// Printed after stated duration
// by AfterFunc() method is over
fmt.Println("6 seconds over....")
// loop stops at this point
mychan <- 30
})
// Calling for loop
for {
// Select statement
select {
// Case statement
case n := <-mychan:
// Printed after the loop stops
fmt.Println(n, "is arriving")
fmt.Println("Done!")
return
// Returned by default
default:
// Printed until the loop stops
fmt.Println("time to wait")
// Sleeps for 3 seconds
time.Sleep(3 * time.Second)
}
}
}
Output:
time to wait
time to wait
6 seconds over....
30 is arriving
Done!
In the above example, after the stated duration is over then channel returns its output and the program exits.
Similar Reads
time.After() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The After() function in Go language is used to wait for the duration of time to pass and after that it delivers the actual time on the returned channel. Moreover, this function is defined under the time pac
3 min read
time.Date() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Date() function in Go language is used to find Date the Time which is equivalent to yyyy-mm-dd hh:mm:ss + nsec nanoseconds in the proper time zone in the stated location. Moreover, this function is defi
2 min read
time.Time.After() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Time.After() function in Go language is used to check if the stated time instant t is after the stated u or not. Moreover, this function is defined under the time package. Here, you need to import the "
2 min read
time.Time.Date() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Time.Date() function in Go language is used to check the year, month, and day in which the stated "t" presents itself. Moreover, this function is defined under the time package. Here, you need to import
2 min read
time.Time.AddDate() Function in Golang with Examples In Go language, time packages supplies functionality for determining as well as viewing time. The Time.AddDate() function in Go language is used to check the time which is equivalent to adding the stated number of years, months, and days to the given "t". For example, if the parameters of the method
3 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