Andrew Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 1 | # Introduction |
| 2 | |
| 3 | Sometimes 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 | |
| 7 | The following code demonstrates a program that waits for an interrupt signal and removes a temporary file when it occurs. |
| 8 | |
| 9 | ``` |
Dave Day | 0d6986a | 2014-12-10 15:02:18 +1100 | [diff] [blame] | 10 | package main |
| 11 | |
| 12 | import ( |
| 13 | "io/ioutil" |
| 14 | "os" |
| 15 | "os/signal" |
| 16 | ) |
| 17 | |
| 18 | func 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 Gerrand | 5bc444d | 2014-12-10 11:35:11 +1100 | [diff] [blame] | 28 | ``` |