Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Example Code

Code Block
func main() {

	ctx := context.Background()

  	// trap Ctrl+C and call cancel on the context
  	ctx, cancel := context.WithCancel(ctx)
  	c := make(chan os.Signal, 1)
  	signal.Notify(c, os.Interrupt)

  	defer func() {
    	signal.Stop(c)
    	cancel()
  	}()

  	go func() {
    	select {
      	case <-c:
      		cancel()
      	case <-ctx.Done():
    	}
  	}()

  	doSomethingAwesome(ctx)

}



References

...