github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/event/example_subscription_test.go (about)

     1  package event_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/neatlab/neatio/utilities/event"
     7  )
     8  
     9  func ExampleNewSubscription() {
    10  
    11  	ch := make(chan int)
    12  	sub := event.NewSubscription(func(quit <-chan struct{}) error {
    13  		for i := 0; i < 10; i++ {
    14  			select {
    15  			case ch <- i:
    16  			case <-quit:
    17  				fmt.Println("unsubscribed")
    18  				return nil
    19  			}
    20  		}
    21  		return nil
    22  	})
    23  
    24  	for i := range ch {
    25  		fmt.Println(i)
    26  		if i == 4 {
    27  			sub.Unsubscribe()
    28  			break
    29  		}
    30  	}
    31  
    32  }