脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - golang等待触发事件的实例

golang等待触发事件的实例

2021-03-18 00:55思维的深度 Golang

这篇文章主要介绍了golang等待触发事件的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 sync.RWMutex
 m map[uint64]chan interface{}
}
 
func Newlist() Wait {
 return &list{m : make(map[uint64]chan interface{})}
}
 
//注册
func (w *list) Register(id uint64) <-chan interface{} {
 w.l.Lock()
 defer w.l.Unlock()
 ch := w.m[id]
 if ch != nil {
  log.Fatal("dup id error")
  return nil
 }
 ch = make(chan interface{},1)
 w.m[id] = ch
 return ch
}
 
//触发
func (w *list) Trigger(id uint64, x interface{}) {
 w.l.Lock()
 ch := w.m[id]
 delete(w.m,id)
 w.l.Unlock()
 if ch != nil {
  ch <- x
  close(ch)
 }
}
 
//判断该id是否被注册
func (w *list) IsRegistered(id uint64) bool {
 w.l.RLock()
 defer w.l.Unlock()
 _,ok := w.m[id]
 return ok
}

示例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var timeOutDuration = time.Minute*10
func main() {
    list := Newlist()
    rid := uint64(time.Now().UnixNano())
    go func() {
        ch := list.Register(rid)
        fmt.Println("start register:",rid)
        if ch == nil {
            return
        }
        select {
        case x := <- ch:
            fmt.Printf("trigger over id:%d,x:%v\n",rid,x)
        case <-time.After(timeOutDuration):
            log.Println("timeout error:",rid)
        }
    }()
    time.Sleep(time.Second)
 
    rid2 := uint64(time.Now().UnixNano())
    go func() {
        ch := list.Register(rid2)
        fmt.Println("start register:",rid2)
        if ch == nil {
            return
        }
        select {
        case x := <- ch:
            fmt.Printf("trigger over id:%d,x:%v\n",rid2,x)
        case <-time.After(timeOutDuration):
            log.Println("timeout error:",rid2)
        }
    }()
 
    go func() {
        time.Sleep(time.Second*5)
        list.Trigger(rid,"Hello")
        time.Sleep(time.Second*3)
        list.Trigger(rid2,"World")
    }()
 
    select {
 
    }
}

补充:GO 程序等待一段时间执行

我就废话不多说了,大家还是直接看代码吧~

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package main
import (
 "fmt"
 "time"
)
 
func main() {
 fmt.Println(time.Now())
 
 //等1秒
 time.Sleep(time.Second * 1)
 fmt.Println(time.Now())
 
 //等1秒
 <-time.After(time.Second * 1)
 fmt.Println(time.Now())
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://skaygo.blog.csdn.net/article/details/81842163

延伸 · 阅读

精彩推荐
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21