Waiting a certain amount of time
The previous recipe describes how to execute the code periodically. This recipe will show you how to execute the code with a delay.
How to do it...
- Open the console and create the folder
chapter04/recipe10
. - Navigate to the directory.
- Create the
delay.go
file with the following content:
package main import ( "fmt" "sync" "time" ) func main() { t := time.NewTimer(3 * time.Second) fmt.Printf("Start waiting at %v\n", time.Now().Format(time.UnixDate)) <-t.C fmt.Printf("Code executed at %v\n", time.Now().Format(time.UnixDate)) wg := &sync.WaitGroup{} wg.Add(1) fmt.Printf("Start waiting for AfterFunc at %v\n", time.Now().Format(time.UnixDate)) time.AfterFunc(3*time.Second, func() { fmt.Printf("Code executed for AfterFunc at %v\n", ...