Created
September 19, 2022 06:52
-
-
Save iamspark1e/8ced2feedca60e58e49e4267973d7c93 to your computer and use it in GitHub Desktop.
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 ( | |
| "fmt" | |
| "runtime" | |
| "time" | |
| ) | |
| func main() { | |
| ticker := time.NewTicker(200 * time.Millisecond) | |
| done := make(chan bool) | |
| go func() { | |
| for { | |
| select { | |
| case <-done: | |
| return | |
| case t := <-ticker.C: | |
| fmt.Println("Tick at", t) | |
| } | |
| } | |
| // runtime.Goexit() | |
| }() | |
| time.AfterFunc(time.Second, func() { | |
| fmt.Println("run ticker.Stop() at " + time.Now().String()) | |
| ticker.Stop() | |
| done <- true | |
| }) | |
| time.Sleep(2 * time.Second) | |
| fmt.Println(runtime.NumGoroutine()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment