Skip to content

Instantly share code, notes, and snippets.

@guanhui07
Created October 14, 2019 01:47
Show Gist options
  • Save guanhui07/88678208bbfee430d4d1ff4e91d549c4 to your computer and use it in GitHub Desktop.
Save guanhui07/88678208bbfee430d4d1ff4e91d549c4 to your computer and use it in GitHub Desktop.
go捕获Ctrl+C信号
package main
import (
"os"
"os/signal"
"syscall"
"fmt"
)
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
//设置要接收的信号
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
fmt.Println()
fmt.Println(sig)
done <- true
}()
fmt.Println("等待信号")
<-done
fmt.Println("进程被终止")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment