Golang
Golang

 

Introduction

Flash sales are exciting events where limited-time discounts are offered to attract customers.
A countdown timer is an effective way to create urgency and excitement. In this tutorial, we will
create a Flash Sale Timer using the Go programming language. This timer will count down from a
specified time and display the remaining time until the flash sale ends.

Objective

The main goal of this program is to build a countdown timer in Go that can be used for flash sales.
The timer will display the remaining time in hours, minutes, and seconds. The program will update the
countdown every second, allowing customers to see the real-time progress until the sale ends.

Code Example


package main

import (
    "fmt"
    "time"
)

func main() {
    // Set the flash sale end time (current time + 1 hour)
    saleEndTime := time.Now().Add(1 * time.Hour)
    
    fmt.Println("Flash Sale Countdown Timer:")
    fmt.Printf("Sale ends at: %v\n", saleEndTime.Format("15:04:05"))

    // Start countdown
    for {
        // Get the current time
        currentTime := time.Now()

        // Calculate the difference between the sale end time and the current time
        remainingTime := saleEndTime.Sub(currentTime)

        if remainingTime <= 0 {
            fmt.Println("The flash sale has ended!")
            break
        }

        // Display remaining time
        fmt.Printf("\rTime remaining: %v", remainingTime)

        // Wait for 1 second before updating
        time.Sleep(1 * time.Second)
    }
}
            

Explanation of the Program

This Go program uses the time package to calculate and display a countdown timer for a flash sale.
Here’s a breakdown of the main sections of the program:

  • Sale End Time: The sale end time is set to 1 hour from the current time using time.Now().Add(1 * time.Hour).
  • Countdown Loop: The program enters a loop that continuously checks the remaining time by subtracting the current time from the sale end time.
  • Time Formatting: The remaining time is displayed in the format hh:mm:ss</>. It updates every second using time.Sleep(1 * time.Second).
  • Sale End Notification: Once the remaining time reaches zero, the program prints a message indicating that the flash sale has ended and the loop terminates.

How to Run the Program

To run the Flash Sale Countdown Timer program, follow these steps:

  1. Install Go programming language on your computer if it’s not already installed. You can download it from golang.org.
  2. Create a new file, e.g., flash_sale_timer.go, and copy the code provided above into the file.
  3. Open a terminal (or command prompt) and navigate to the folder containing your flash_sale_timer.go file.
  4. Run the program by typing the following command in the terminal:
    go run flash_sale_timer.go
  5. The countdown timer will start in the terminal, showing the remaining time for the flash sale.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)