Last active
November 15, 2020 09:32
-
-
Save Yougigun/caf61b01adbccba36abfdfef0ba832ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
fmt.Println("error check 1:", ctx.Err()) | |
fmt.Println("num gortins 1:", runtime.NumGoroutine()) | |
go func() { | |
n := 0 | |
for { | |
select { | |
case <-ctx.Done(): | |
return | |
default: | |
n++ | |
time.Sleep(time.Millisecond * 200) | |
fmt.Println("working", n) | |
} | |
} | |
}() | |
time.Sleep(time.Second * 2) | |
fmt.Println("error check 2:", ctx.Err()) | |
fmt.Println("num gortins 2:", runtime.NumGoroutine()) | |
fmt.Println("about to cancel context") | |
cancel() | |
fmt.Println("cancelled context") | |
time.Sleep(time.Second * 2) | |
fmt.Println("error check 3:", ctx.Err()) | |
fmt.Println("num gortins 3:", runtime.NumGoroutine()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment