github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/event/example_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:38</date>
    10  //</624450090999746560>
    11  
    12  
    13  package event
    14  
    15  import "fmt"
    16  
    17  func ExampleTypeMux() {
    18  	type someEvent struct{ I int }
    19  	type otherEvent struct{ S string }
    20  	type yetAnotherEvent struct{ X, Y int }
    21  
    22  	var mux TypeMux
    23  
    24  //Start a subscriber.
    25  	done := make(chan struct{})
    26  	sub := mux.Subscribe(someEvent{}, otherEvent{})
    27  	go func() {
    28  		for event := range sub.Chan() {
    29  			fmt.Printf("Received: %#v\n", event.Data)
    30  		}
    31  		fmt.Println("done")
    32  		close(done)
    33  	}()
    34  
    35  //发布一些事件。
    36  	mux.Post(someEvent{5})
    37  	mux.Post(yetAnotherEvent{X: 3, Y: 4})
    38  	mux.Post(someEvent{6})
    39  	mux.Post(otherEvent{"whoa"})
    40  
    41  //stop关闭所有订阅通道。
    42  //订户GOUDOTIN将打印“完成”
    43  //然后退出。
    44  	mux.Stop()
    45  
    46  //等待订阅服务器返回。
    47  	<-done
    48  
    49  //输出:
    50  //接收:事件。某些事件i:5
    51  //接收:事件。某些事件i:6
    52  //事件:其他事件{s:“哇”}
    53  //完成
    54  }
    55