alexballas / ctxreader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Context-aware io.Reader Go Reference

Example:

package main

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

// This example shows how to use NewContextReader.
func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	// create the context reader.
	cr := ctxreader.NewContextReader(ctx, os.Stdin)

	// read our new reader.
	buf := bufio.NewScanner(cr)
	fmt.Println("You have 5 seconds to enter your input:")
	buf.Scan()

	switch buf.Err() {
	case nil:
		fmt.Println("You entered:", buf.Text())
	default:
		fmt.Println("Time is up!", buf.Err())
	}
}

About

License:MIT License


Languages

Language:Go 100.0%