Skip to content

Instantly share code, notes, and snippets.

@Yougigun
Last active November 15, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yougigun/caf61b01adbccba36abfdfef0ba832ee to your computer and use it in GitHub Desktop.
Save Yougigun/caf61b01adbccba36abfdfef0ba832ee to your computer and use it in GitHub Desktop.
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