My codes:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func f() {
// Simulating a function that might take some time to complete
fmt.Println("Function f is starting")
time.Sleep(5 * time.Second) // Simulate work
fmt.Println("Function f is completed")
return
}
func test() {
// Setting up a channel to listen to signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Channel to control the termination of the main loop
done := make(chan bool, 1)
done2 := make(chan bool, 1)
go func() {
sig := <-sigChan
fmt.Printf("Received signal: %v, waiting for function f to finishn", sig)
done <- true
done2 <- true
}()
go func() {
for {
select {
case <-done2:
fmt.Println("Exiting service status reporting loop of Pipeline")
return
default:
fmt.Println("reporting start")
time.Sleep(5 * time.Second)
fmt.Println("reporting end")
}
}
}()
for {
select {
case <-done:
fmt.Println("Exiting main loop")
//forever <- struct{}{}
return
default:
f()
}
}
fmt.Println("all done.........")
}
func main() {
test()
}
i hope ctrl+C can stop the main for loop and the for loop in the goroutine.
but it seems that “all done……..” never printed out.
any idea? thanks