goemitter/goemitter_test/main.go

45 lines
801 B
Go
Raw Normal View History

2023-11-29 23:48:57 +08:00
package main
import (
"fmt"
2024-09-19 16:41:07 +08:00
"log"
"runtime/debug"
"time"
2023-11-29 23:48:57 +08:00
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")
2024-09-19 16:41:07 +08:00
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in main:", r)
// 打印当前所有 goroutine 的堆栈信息
log.Println(string(debug.Stack()))
}
}()
time.Sleep(1 * time.Second)
2023-11-29 23:48:57 +08:00
}