// Our communication channel is created
done := make(chan interface{})
// run this function as goroutine
go func() {
// wait/sleep 5 seconds
time.Sleep(5 * time.Second)
// close our communication channel
close(done)
}()
// initialize workcount with 0
workcount := 0
loop:
// loop
for {
// read channel data
select {
// if channel is closed break this loop, break = stop!
case <-done:
break loop
// default do nothing
default:
}
// if our channel doesn't send anything, just add +1 to workcount
workcount++
// print workcount
fmt.Println(workcount)
// wait 1 second before we run this loop again
time.Sleep(1 * time.Second)
}
// so workcount is 5, cuz our goroutine will send term signal after 5 seconds
fmt.Printf("Achieved %v cycles of work before signalled to stop
", workcount)
A much cleaner way to solve this issue would be
package main
import (
"fmt"
"time"
)
func main() {
// sends after 5 seconds to this channel https://golang.org/pkg/time/#After
timeout := time.After(5 * time.Second)
// sends each second to this channel https://golang.org/pkg/time/#Tick
tick := time.Tick(1 * time.Second)
// our workcount var
workcount := 0
// for infinite
for {
// waits for data on each channel
select {
// fired if time.After wrote in timeout
case <-timeout:
fmt.Printf("Achieved %v cycles of work before signalled to stop
", workcount)
return
// fired if time.Tick wrote in tick
case <-tick:
workcount++
fmt.Println(workcount)
}
}
}
You run your code in main function so we need to return. We gonna fix this code using return
package main
import (
"fmt"
"time"
)
func main() {
// Our communication channel is created
done := make(chan interface{})
// run this function as goroutine
go func() {
// wait/sleep 5 seconds
time.Sleep(5 * time.Second)
// close our communication channel
close(done)
}()
// initialize workcount with 0
workcount := 0
// loop
for {
// read channel data
select {
// if channel is closed break this loop, break = stop!
case <-done:
fmt.Printf("Achieved %v cycles of work before signalled to stop
", workcount)
return
// default do nothing
default:
}
// if our channel doesn't send anything, just add +1 to workcount
workcount++
// print workcount
fmt.Println(workcount)
// wait 1 second before we run this loop again
time.Sleep(1 * time.Second)
}
}