Created
July 22, 2019 03:50
-
-
Save yifanes/045307af41511972b747b037809358bb to your computer and use it in GitHub Desktop.
golang的context
This file contains hidden or 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" | |
"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