Skip to main content

Command Palette

Search for a command to run...

It's Time for time package in GO

Updated
2 min read
It's Time for time package in GO

The time package offers the Time datatype, which represents an instant in time. It also includes several functions for extracting and formatting different components of a timestamp. For example, you can get the current time using time.Now(), and then extract specific parts like the day, month, or year using methods such as t.Day(), t.Month(), and t.Year().

Here’s an example that formats the current date:

t := time.Now()
fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year()) // Output: 29.10.2019

Working with Durations

The Duration type represents the time elapsed between two instants, measured in nanoseconds (int64). You can calculate the duration between two time points or add durations to a time. For instance, the function Since(t Time) returns the time elapsed since t.

Here’s an example of adding one week to the current time:

t := time.Now()
week := time.Duration(60 * 60 * 24 * 7 * 1e9) // One week in nanoseconds
t = t.Add(week)
fmt.Println(t.Format("02 Jan 2006 15:04")) // Output: time one week from now

Formatting Time

Go's time package provides predefined formats, but you can also create custom formats. For example:

t := time.Now().UTC()
fmt.Println(t.Format("02 Jan 2006 15:04")) // Output: 29 Oct 2019 11:00

You can also use formats like time.RFC822 or time.ANSIC for commonly used formats:

fmt.Println(t.Format(time.RFC822))  // e.g., 30 Oct 19 11:34 UTC
fmt.Println(t.Format(time.ANSIC))   // e.g., Wed Oct 30 11:34:03 2019

Mocking Asynchronous Behavior with time.Sleep()

In real-world applications, there are often asynchronous operations (like fetching data from an API). To simulate or mock asynchronous behaviour in Go, you can use time.Sleep() to pause execution for a specified duration. This is useful in testing or demonstrating delayed operations.

Here’s an example of using time.Sleep() to mock an asynchronous process:

fmt.Println("Starting task...")
time.Sleep(2 * time.Second) // Simulate a task that takes 2 seconds
fmt.Println("Task completed!")

In this example, the program simulates a delay of 2 seconds before completing the task. This can be helpful when testing the timing of functions or mimicking the delay of external services in concurrent or parallel routines.

With the time.Sleep() function, you can easily simulate delays or pauses in your program, making it a handy tool for testing or mocking asynchronous operations.


In the next lesson, we’ll cover pointers in Go!

24 views

Learn GO

Part 2 of 13

In this series, I will write articles that help you understand go! I am learning go so I am writing this series as a note for myself and hope you also love this series and learn along with me!

Up next

Strings in Go - Advanced

In this guide, we'll explore how to work with strings and the strconv package in Go, covering useful functions that help manipulate and convert strings. Common String Operations Go provides several built-in functions in the strings package that allow...