From cf32733725760ebb0a3335d439347f409f113799 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Thu, 14 Aug 2025 00:35:04 +0800 Subject: [PATCH] event: using slices.Index in std lib --- event/event.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/event/event.go b/event/event.go index 25a2c2e457..e3a4502fb2 100644 --- a/event/event.go +++ b/event/event.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "reflect" + "slices" "sync" "time" ) @@ -66,7 +67,7 @@ func (mux *TypeMux) Subscribe(types ...interface{}) *TypeMuxSubscription { for _, t := range types { rtyp := reflect.TypeOf(t) oldsubs := mux.subm[rtyp] - if find(oldsubs, sub) != -1 { + if slices.Index(oldsubs, sub) != -1 { panic(fmt.Sprintf("event: duplicate type %s in Subscribe", rtyp)) } subs := make([]*TypeMuxSubscription, len(oldsubs)+1) @@ -118,7 +119,7 @@ func (mux *TypeMux) del(s *TypeMuxSubscription) { mux.mutex.Lock() defer mux.mutex.Unlock() for typ, subs := range mux.subm { - if pos := find(subs, s); pos >= 0 { + if pos := slices.Index(subs, s); pos >= 0 { if len(subs) == 1 { delete(mux.subm, typ) } else { @@ -128,15 +129,6 @@ func (mux *TypeMux) del(s *TypeMuxSubscription) { } } -func find(slice []*TypeMuxSubscription, item *TypeMuxSubscription) int { - for i, v := range slice { - if v == item { - return i - } - } - return -1 -} - func posdelete(slice []*TypeMuxSubscription, pos int) []*TypeMuxSubscription { news := make([]*TypeMuxSubscription, len(slice)-1) copy(news[:pos], slice[:pos])