Go信号实例

有时我们希望Go程序能够智能地处理Unix信号。 例如,可能希望服务器在接收到SIGTERM时正常关闭,或者在收到SIGINT时使用命令行工具停止处理输入。下面介绍如何使用Go语言处理信号。

Go信号通知通过在通道上发送os.Signal值来工作。创建一个通道来接收这些通知(还会在程序退出时通知我们)。

所有的示例代码,都放在 F:\worksp\golang 目录下。安装Go编程环境请参考:/tutorial/detail-5562.html

signal.go的完整代码如下所示 -

package main

import "fmt"
import "os"
import "os/signal"
import "syscall"

func main() {

    // Go signal notification works by sending `os.Signal`
    // values on a channel. We'll create a channel to
    // receive these notifications (we'll also make one to
    // notify us when the program can exit).
    sigs := make(chan os.Signal, 1)
    done := make(chan bool, 1)

    // `signal.Notify` registers the given channel to
    // receive notifications of the specified signals.
    signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

    // This goroutine executes a blocking receive for
    // signals. When it gets one it'll print it out
    // and then notify the program that it can finish.
    go func() {
        sig := <-sigs
        fmt.Println()
        fmt.Println(sig)
        done <- true
    }()

    // The program will wait here until it gets the
    // expected signal (as indicated by the goroutine
    // above sending a value on `done`) and then exit.
    fmt.Println("awaiting signal")
    <-done
    fmt.Println("exiting")
}

执行上面代码,将得到以下输出结果 -

F:\worksp\golang>go run signal.go
awaiting signal
[Ctl+C]
interrupt
exiting

上一篇:Go执行过程实例

下一篇:Go退出程序实例

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程