I won't say much nonsense, let's just read the code~
type Wait interface { // Register waits returns a chan that waits on the given ID. // The chan will be triggered when Trigger is called with // the same ID. Register(id uint64) <-chan interface{} // Trigger triggers the waiting chans with the given ID. Trigger(id uint64, x interface{}) IsRegistered(id uint64) bool } type list struct { l m map[uint64]chan interface{} } func Newlist() Wait { return &list{m : make(map[uint64]chan interface{})} } //registerfunc (w *list) Register(id uint64) <-chan interface{} { () defer () ch := [id] if ch != nil { ("dup id error") return nil } ch = make(chan interface{},1) [id] = ch return ch } //triggerfunc (w *list) Trigger(id uint64, x interface{}) { () ch := [id] delete(,id) () if ch != nil { ch <- x close(ch) } } //Discern whether the id is registeredfunc (w *list) IsRegistered(id uint64) bool { () defer () _,ok := [id] return ok }
Example
var timeOutDuration = *10 func main() { list := Newlist() rid := uint64(().UnixNano()) go func() { ch := (rid) ("start register:",rid) if ch == nil { return } select { case x := <- ch: ("trigger over id:%d,x:%v\n",rid,x) case <-(timeOutDuration): ("timeout error:",rid) } }() () rid2 := uint64(().UnixNano()) go func() { ch := (rid2) ("start register:",rid2) if ch == nil { return } select { case x := <- ch: ("trigger over id:%d,x:%v\n",rid2,x) case <-(timeOutDuration): ("timeout error:",rid2) } }() go func() { (*5) (rid,"Hello") (*3) (rid2,"World") }() select { } }
Additional: GO program is waiting for a while to execute
I won't say much nonsense, let's just read the code~
package main import ( "fmt" "time" ) func main() { (()) //Wait for 1 second ( * 1) (()) //Wait for 1 second <-( * 1) (()) }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.