go 使用signal包进行信号的监听基础
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
)
func main(){
c:=make(chan os.Signal)
signal.Notify(c)
go func() {
for {
fmt.Println(12)
time.Sleep(1e9)
}
}()
go func() {
time.AfterFunc(5e9, func() {
c<-os.Interrupt
})
}()
go func() {
ctx, _ := context.WithTimeout(context.Background(), 4e9)
<-ctx.Done()
c<-os.Kill
//select {
// case <-ctx.Done():
// c<-os.Kill
//}
}()
sig:=<-c
fmt.Println(sig)//^Cinterrupt
}
Tags : 本文未设置标签
您可以自由的转载和修改,但请务必注明文章来源并且不可用于商业目的。
本站大部分内容收集于互联网,如果有侵权内容、不妥之处,请联系删除。敬请谅解!
Previous post
syslog-ng 远程收集日志
Next post
无聊抓包玩