PracticeDev/study_go/context/main.go

28 lines
492 B
Go
Raw Normal View History

2022-12-20 17:31:11 +08:00
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
go Speak(ctx)
time.Sleep(10 * time.Second)
cancel()
time.Sleep(1 * time.Second)
}
func Speak(ctx context.Context) {
for range time.Tick(time.Second) {
select {
case <-ctx.Done():
fmt.Println("我要闭嘴了")
return
default:
fmt.Println("balabalabalabala")
}
}
}