Open In App

time.AfterFunc() Function in Golang With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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:




// 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.



Last Updated : 21 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads