直接上代码
package main
import (
"runtime"
"sync"
"os"
"os/signal"
"syscall"
"fmt"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
wg := &sync.WaitGroup{}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
sig := <-c
_ = sig
wg.Done()
}()
wg.Add(1)
fmt.Println("执行任务~~~")
wg.Wait()
fmt.Printf("结束")
}
运行代码
执行任务~~~
点击停止按钮
执行任务~~~
结束
Process finished with exit code 0
有疑问加站长微信联系(非本文作者)