package main

import (
	"context"
	"fmt"
	"math/rand"
	"time"
)

type RandSum struct{}

func main() {
	sum := 0
	ctx, cancel := context.WithTimeout(context.Background(), time.Duration(2)*time.Second)
	defer cancel()
	start := time.Now()
	i := 0
outLoop:
	for {
		select {
		case <-ctx.Done():
			ctx = context.WithValue(ctx, RandSum{}, "timeout")
			break outLoop
		default:
			i++
			randNum := rand.Int() % 100_000_000
			//fmt.Printf("Randnum is %d\n", randNum)
			sum = sum + randNum
			if randNum == 1234 {
				ctx = context.WithValue(ctx, RandSum{}, "number reached")
				break outLoop
			}
		}
	}
	fmt.Printf("Sum is %d after %v; iterations - %d; cause - %s\n", sum, time.Since(start), i, ctx.Value(RandSum{}))
}
