Исходники и презентации
This commit is contained in:
29
lessons/contexts/after_done/main.go
Normal file
29
lessons/contexts/after_done/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// context.AfterFunc
|
||||
|
||||
func WithCtxAfterFunc(ctx context.Context, action func()) {
|
||||
if action != nil {
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
action()
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
WithCtxAfterFunc(ctx, func() {
|
||||
fmt.Println("after")
|
||||
})
|
||||
|
||||
cancel()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
Reference in New Issue
Block a user