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 ¶
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.
Click to show internal directories.
Click to hide internal directories.