blob: 6d1159d4289645a478b3139faa58767367719208 [file] [log] [blame] [view]
Andrew Gerrand5bc444d2014-12-10 11:35:11 +11001# Introduction
2
3Sometimes an application needs to save internal state or perform some cleanup activity before it exits, or needs to be able to reload a configuration file or write a memory/cpu profile on demand. In UNIX-like operating systems, signals can accomplish these tasks.
4
5# Example
6
7The following code demonstrates a program that waits for an interrupt signal and removes a temporary file when it occurs.
8
Younis Shahd84ea992017-09-25 16:51:33 +05309```go
Dave Day0d6986a2014-12-10 15:02:18 +110010package main
11
12import (
13 "io/ioutil"
14 "os"
15 "os/signal"
16)
17
18func main() {
19 f, err := ioutil.TempFile("", "test")
20 if err != nil {
21 panic(err)
22 }
23 defer os.Remove(f.Name())
24 sig := make(chan os.Signal, 1)
25 signal.Notify(sig, os.Interrupt)
26 <-sig
27}
Andrew Gerrand5bc444d2014-12-10 11:35:11 +110028```