Context WithDeadline dan Context WithTimeout
Contoh code context WithDeadline
package main
import (
"context"
"fmt"
"time"
)
func main() {
deadline := time.Now().Add(1 * time.Millisecond)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
select {
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err())
}
}Contoh code context WithTimeout
Last updated