O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter04/recipe10.
  2. Navigate to the directory.
  3. 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",                      time.Now().Format(time.UnixDate))          wg.Done()        })        wg.Wait()        fmt.Printf("Waiting on time.After at %v\n",  time.Now().Format(time.UnixDate)) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required