Skip to content

Latest commit

 

History

History

004_using_time_after

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Go Sample Example - Using time.After

This repository demonstrates how to use the time.After function in Go. It includes an example of creating a simple timer that waits for a specified duration and fires once, providing a straightforward alternative to time.NewTimer.

📖 Information

  • This example covers how to use the time.After function in Go to wait for a specific duration.
  • The time.After function returns a channel that will receive the current time after the specified duration has passed.

💻 Code Example

package main

import (
	"fmt"
	"time"
)

func main() {

	// Using time.After
	// time.After is a simpler alternative to time.NewTimer for creating a timer that only needs to fire once.
	// It returns a channel that will receive the current time after the specified duration.

	fmt.Println("Waiting for 2 seconds...")

	// Wait for 2 seconds using time.After
	<-time.After(2 * time.Second)

	fmt.Println("2 seconds passed.")
}

🏃 How to Run

  1. Make sure you have Go installed. If not, you can download it from here.

  2. Clone this repository:

    git clone https://github.com/Rapter1990/go_sample_examples.git
  3. Navigate to the 004_using_time_after directory:

    cd go_sample_examples/020_timers/004_using_time_after
  4. Run the Go program:

    go run 004_using_time_after.go

📦 Output

When you run the program, you should see the following output:

Waiting for 2 seconds...
2 seconds passed.