...

Source file src/os/signal/example_test.go

Documentation: os/signal

		 1  // Copyright 2013 The Go Authors. All rights reserved.
		 2  // Use of this source code is governed by a BSD-style
		 3  // license that can be found in the LICENSE file.
		 4  
		 5  package signal_test
		 6  
		 7  import (
		 8  	"fmt"
		 9  	"os"
		10  	"os/signal"
		11  )
		12  
		13  func ExampleNotify() {
		14  	// Set up channel on which to send signal notifications.
		15  	// We must use a buffered channel or risk missing the signal
		16  	// if we're not ready to receive when the signal is sent.
		17  	c := make(chan os.Signal, 1)
		18  	signal.Notify(c, os.Interrupt)
		19  
		20  	// Block until a signal is received.
		21  	s := <-c
		22  	fmt.Println("Got signal:", s)
		23  }
		24  
		25  func ExampleNotify_allSignals() {
		26  	// Set up channel on which to send signal notifications.
		27  	// We must use a buffered channel or risk missing the signal
		28  	// if we're not ready to receive when the signal is sent.
		29  	c := make(chan os.Signal, 1)
		30  
		31  	// Passing no signals to Notify means that
		32  	// all signals will be sent to the channel.
		33  	signal.Notify(c)
		34  
		35  	// Block until any signal is received.
		36  	s := <-c
		37  	fmt.Println("Got signal:", s)
		38  }
		39  

View as plain text