Understanding Time Management in Go: Timer, Ticker, and After

Kacper Bąk
3 min readMay 15, 2023

Go, also known as Golang, is an open-source programming language that makes it easy to build simple, reliable, and efficient software. Among the many features it provides, Go offers built-in support for managing time via the time package. This package provides functionality for measuring and displaying time, but it also contains three important constructs for time-based operations: time.After, Timer, and Ticker. In this article, we will delve into these three concepts, and we will also use UML diagrams to visualize their behavior and relationships.

Photo by Nathan Dumlao on Unsplash

The time Package

The time package in Go provides a set of time-related functions and types. It allows for operations like comparing times, formatting times, calculating the duration between two times, and so on. For our discussion, the critical part of the time package is the Timer, Ticker, and time.After functionalities.

time.After

The time.After function in Go is a handy function that returns a channel that will send the current time after the specified duration. This is particularly useful when you want to do something after a delay.

select {
case <-time.After(time.Second):
fmt.Println("One second has passed")
}

--

--

Kacper Bąk
Kacper Bąk

Written by Kacper Bąk

Software Engineer & Backend Developer

No responses yet