Skip to content

Instantly share code, notes, and snippets.

@yifanes
Created July 22, 2019 03:50
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 yifanes/045307af41511972b747b037809358bb to your computer and use it in GitHub Desktop.
Save yifanes/045307af41511972b747b037809358bb to your computer and use it in GitHub Desktop.
golang的context
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 3 * time.Second)
defer cancel()
go func() {
for range time.Tick(time.Second) {
select {
case <-ctx.Done():
return
default:
fmt.Println("monitor working")
}
}
}()
time.Sleep(time.Second * 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment