February 2018
Intermediate to advanced
340 pages
9h 43m
English
package main import ( "fmt" "os" "os/signal" "time" ) func main() { c := make(chan os.Signal, 1) signal.Notify(c) ticker := time.NewTicker(time.Second) stop := make(chan bool) go func() { defer func() { stop <- true }() for { select { case <-ticker.C: fmt.Println("Tick") case <-stop: fmt.Println("Goroutine closing") return } } }() // Block until // the signal is received <-c ticker.Stop() // Stop the goroutine stop <- true // Wait until the <-stop fmt.Println("Application stopped") }
Read now
Unlock full access