time

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package time provides context-aware time utilities extending the standard time package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Sleep

func Sleep(ctx context.Context, d time.Duration) error

Sleep waits for the specified delay duration or until the context is canceled, whichever occurs first. Returns an error if the context is canceled before the delay elapses.

Example
package main

import (
	"context"
	"fmt"
	"time"

	gotime "github.com/foomo/go/time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel()

	// Wait for 1 second (completes successfully)
	err := gotime.Sleep(ctx, 1*time.Second)
	if err != nil {
		fmt.Println("Sleep failed:", err)
		return
	}

	fmt.Println("Sleep completed successfully")

	// Sleep for 3 seconds with a 2-second timeout (context cancels first)
	ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
	defer cancel2()

	err = gotime.Sleep(ctx2, 3*time.Second)
	if err != nil {
		fmt.Println("Sleep cancelled:", err)
	}

}
Output:
Sleep completed successfully
Sleep cancelled: context deadline exceeded

func WaitFor added in v0.11.0

func WaitFor(ctx context.Context, fn func(context.Context) (bool, error), timeout, interval time.Duration) error

WaitFor repeatedly invokes the provided function until it returns true, an error, or the timeout duration elapses.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL