OS Signals:
OS Signals
suggest changeYou can get notify about signals the os sends to your process.
For example, Ctrl-C sends SIGINT signal. Here's how to get notified about SIGINT signal:
package main
import (
"fmt"
"os"
"os/signal"
)
func main() {
sigChan := make(chan os.Signal)
// assign all signal notifications to the channel
signal.Notify(sigChan)
// blocks until you get a signal from the OS
select {
case sig := <-sigChan:
fmt.Println("Received signal from OS:", sig)
}
}
This program creates a channel, uses signal.Notify to tell Go runtime to send a message to the channel when the OS sends a signal to the process.
$ go run signals.go
^CReceived signal from OS: interrupt
The ^C above is the keyboard command CTRL+C which sends the SIGINT signal.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents
4
Strings
5
Pointers
6
Arrays
7
Slices
8
Maps
9
Structs
10
Interfaces
15
Functions
16
Methods
18
Defer
20
Concurrency
22
Mutex
23
Packages
27
Logging
30
JSON
31
XML
32
CSV
33
YAML
34
SQL
35
HTTP Client
36
HTTP Server
38
Reflection
39
Context
40
Package fmt
41
OS Signals
42
Testing
49
gob
50
Plugin
53
Console I/O
54
Cryptography
59
Contributors