Golang time.Time.AfterFunc() function example
package time
Golang time.Time.AfterFunc() function usage example. Useful in situation where you want to execute tasks after a certain time lapsed.
package main
import (
"fmt"
"time"
)
func main() {
duration := time.Duration(5) * time.Second
f := func() {
fmt.Println("Called by AfterFunc() after 5 seconds")
}
timer := time.AfterFunc(duration, f)
defer timer.Stop()
// sleep for 1 minute
time.Sleep(time.Minute)
}
Output :
Called by AfterFunc() after 5 seconds
Reference :
Advertisement
Something interesting
Tutorials
+15.9k Golang : Update database with GORM example
+7k Golang : Gargish-English language translator
+10.2k Golang : Random Rune generator
+5.6k PHP : Convert CSV to JSON with YQL example
+22.7k Golang : Round float to precision example
+5.8k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+7.5k Golang : How to handle file size larger than available memory panic issue
+7.7k Gogland : Where to put source code files in package directory for rookie
+16.8k Golang : Get own process identifier
+21.1k Golang : Sort and reverse sort a slice of strings
+6.3k Golang : Selection sort example