September 2017
Intermediate to advanced
466 pages
9h 33m
English
This subsection will present a naive Go program that handles only the SIGTERM and SIGINT signals. The Go code of h1s.go will be presented in three parts; the first part is the following:
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
)
func handleSignal(signal os.Signal) {
fmt.Println("Got", signal)
}
Apart from the preamble of the program, there is also a function named handleSignal() that will be called when the program receives any of the two supported signals.
The second part of h1s.go contains the following Go code:
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
go func() {
for {
sig := <-sigs
fmt.Println(sig)
handleSignal(sig)
}
}()
The ...
Read now
Unlock full access