goemitter/goemitter_test/main.go

45 lines
801 B
Go

package main
import (
"fmt"
"log"
"runtime/debug"
"time"
emitter "git.pyer.club/kingecg/goemitter"
)
type St struct {
Name string
}
func (s *St) Print(data ...interface{}) {
fmt.Println(s.Name)
fmt.Println(data...)
}
func main() {
s := &St{Name: "testeer"}
em := emitter.NewEmitter()
em.On("test", func(data ...interface{}) {
fmt.Println(data...)
})
em.On("test", s.Print)
em.Once("test", func(data ...interface{}) {
s := fmt.Sprint(data...)
fmt.Println(interface{}("Once"), s)
})
em.EmitSync("test", "hello", "world")
em.Emit("test", "hello", "world2")
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in main:", r)
// 打印当前所有 goroutine 的堆栈信息
log.Println(string(debug.Stack()))
}
}()
time.Sleep(1 * time.Second)
}