import ( "fmt" "time" ) func main() { timer := time.NewTimer(2 * time.Second) <-timer.C // waits for the timer to fire fmt.Println("Timer fired") }
import ( "fmt" "time" ) func main() { timer := time.NewTimer(2 * time.Second) go func() { <-timer.C fmt.Println("Timer fired") }() time.Sleep(3 * time.Second) // wait for timer to fire }In this example, we create a new timer that will fire after 2 seconds. However, this time we use a goroutine to wait for the timer to fire so that the main program can continue executing. We then use `time.Sleep` to wait for 3 seconds, which gives the timer enough time to fire. Once it does, we print out a message to the console. Overall, the Go time Timer package library is a handy tool for adding timed events to your Go programs.