time

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

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

Types

This section is empty.

Jump to

Keyboard shortcuts

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