Ready Steady GO: Call Function Periodically With Timeout

This is a golang sample code that calls some function periodically for a specified amount of time.

2020-08-161 min readEN

In this post, I introduce a golang example code that calls some function periodically for a specified amount of time. It was a good exercise for a beginner like me to use goroutines.

The sample code consists of the 3 elements.

  1. Write a function preiodicGreet() that prints a given message periodycally
  2. Set a timeout context in the main function
  3. Call the function preiodicGreet() as a goroutine in the main function
package main

import (
	"context"
	"fmt"
	"time"
)

func periodicGreet(msg string, period time.Duration) {
	t := time.NewTicker(period * time.Second)
	defer t.Stop()
	for {
		select {
		case <-t.C: // Activate periodically
			fmt.Printf(msg)
		}
	}
}

func main() {
	ctx := context.Background()
	ctx, cancel := context.WithTimeout(ctx, 5*time.Second) // Cancel in 5 seconds
	defer cancel()

	go periodicGreet("Hello, World!\n", 1) // Call as a goroutine

	select {
	case <-ctx.Done(): // When time is out
		fmt.Println("Periodic greeting done")
	}
}

References

[1] Goで一定周期で何かを行う方法 - Qiita
[2] contextの使い方 - Qiita

Related posts

Shion Honda

Software engineer based in Rome since February 2026. I work on bringing AI agents into products, and I write about evaluation, context engineering, and the practical edges of LLM systems.