Sobes.tech
Senior

Kas on mingi tähtaeg?

sobes.tech AI

Vastus AI-lt

In Go, a deadline is usually understood as a time constraint on the execution of an operation or context.

In the standard library, the context package is used to manage execution time and cancel operations. A context can have a deadline — a time after which the operation is considered expired and should be canceled.

Example of creating a context with a deadline:

ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(2*time.Second))
defer cancel()

select {
case <-time.After(3 * time.Second):
    fmt.Println("Operation completed")
case <-ctx.Done():
    fmt.Println("Deadline exceeded or context canceled")
}

Thus, a deadline is a way to set a time limit for operations to prevent infinite waiting.