package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
eatNum := chiHanBao(ctx)
for n := range eatNum {
if n >= 10 {
cancel()
break
}
}
fmt.Println("正在统计结果。。。")
time.Sleep(1 * time.Second)
}
func chiHanBao(ctx context.Context) <-chan int {
c := make(chan int)
// 个数
n := 0
// 时间
t := 0
go func() {
for {
//time.Sleep(time.Second)
select {
case <-ctx.Done():
fmt.Printf("耗时 %d 秒,吃了 %d 个汉堡 \n", t, n)
return
case c <- n:
incr := 1
n += incr
if n >= 10 {
n = 10
}
t++
fmt.Printf("我吃了 %d 个汉堡\n", n)
}
}
}()
return c
}