Go: gsignal, Master of Signals

Vincent
A Journey With Go
Published in
3 min readMar 9, 2020

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.13.

The package signal provides signal handlers and allows our Go program to interact with the incoming signals. Let’s start with the listeners before diving into the internals.

Subscription

The subscription to the signals is done via channels. Here is an example of a program that listens to any interruption signal or terminal resizing:

Each os.Signal channel listens to their own set of events. Here is a diagram with the subscription workflow of the previous example:

Go also gives the ability for a channel to stop being notified — function Stop(os.Signal) — or to ignore signals — function Ignore(...os.Signal). Here is a short example of each:

This program cannot be interrupted by CTRL+C and will never stop since the channel…

--

--