Skip to content

Instantly share code, notes, and snippets.

@improbablepiotr
Last active September 12, 2017 09:44
Show Gist options
  • Save improbablepiotr/6be2b69ac98a77f19459a53ac3e11e7c to your computer and use it in GitHub Desktop.
Save improbablepiotr/6be2b69ac98a77f19459a53ac3e11e7c to your computer and use it in GitHub Desktop.
Simple Go program to test CTRL-C or other ways to interrupt it.

To build: go build -o int_test.exe main.go.

To run: int_test.exe.

package main
import "os/signal"
import "os"
import "fmt"
import "time"
func main() {
// Prints "Still alive..." every second
ticker := time.NewTicker(time.Second)
go func() {
for range ticker.C {
fmt.Println("Still alive...")
}
}()
// Prints "Died." when program exits.
defer fmt.Println("Died.")
// Prints "Received signal: " when a signal is received.
c := make(chan os.Signal)
signal.Notify(c)
select {
case s := <-c:
fmt.Printf("Received signal: %v\n", s)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment