context/

directory
v0.0.0-...-280f8e7 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2024 License: CC0-1.0

README

context

Exemplos simples de contexto

import (
	"bufio"
	"context"
	"fmt"
	"os"
	"time"
)

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

	defer cancel()

	go func(ctx context.Context, cancel context.CancelFunc) {
		s := bufio.NewScanner(os.Stdin)
		s.Scan()
		cancel()
	}(ctx, cancel)

	select {
	case <-time.After(5 * time.Second):
		fmt.Println("timeout")
	case <-ctx.Done():
		fmt.Println(ctx.Err())
	}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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