ruk·si

🐹 Go
Signals

Updated at 2022-01-20 22:13

You can listen on process signals like Ctrl+C through signal.Notify().

package main

import (
    "os"
    "os/signal"
    "syscall"
)

func assert(condition bool) {
    if !condition {
        panic("assert failed")
    }
}

func main() {
    signals := make(chan os.Signal, 1)
    signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)

    done := make(chan bool, 1)
    go func() {
        s := <-signals
        assert(s == syscall.SIGINT)
        done <- true
    }()
    <-done // waiting for a Ctrl+C interrupt signal
}