Countdown Timer GoLang Example

Last updated Apr 19, 2021

This Example will show you print a count down number with time delay. This example we used Time packge to make execute the program with time delay. This example print number with delay of 1 second, this delay is done by

time.Sleep(time.Second)

 

Example

package main

import (
"fmt"
"math/rand"
"time"
)

func main() {
  fmt.Println("Initiating rocket launch")
  fmt.Println("Beginning countdown sequence")
  count := 10
  for count > 0 {
          if rand.Intn(100) == 1 {
          break
          }
          fmt.Printf("%v...\n", count)
          time.Sleep(time.Second)
          count--
  }
       if count == 0 {
           fmt.Println("Countdown Executed")
      } else {
          fmt.Println("Countdown failed")
      }
}

 

Output:

Beginning countdown sequence
10...
9...
8...
7...
6...
5...
4...
3...
2...
1...
Countdown Executed

 

 

   Run Program    

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

661 Views