dougu4448 2016-07-26 14:21
浏览 27

使用通道在Go中进行事件处理

I have written an SDK that encapsulates a bunch of logic and functionality.

Typically, I would use event listeners/callbacks to provide feedback to the application using the SDK, but that is not idiomatic in Go.

It is my understanding that channels and goroutines fill the void of callbacks but what is a way to implement this?

Say for example, my SDK has a number of events that I want to notify the main application about. Would I use one channel based on an Event struct. This Event would contain a _type field which specifies the event type, then a data field of type interface{} similar to Object in Java. The receiver would then check the type and cast the data accordingly.

Either the above approach or one channel for event then the app listens for whatever channels it is concerned with.

For the record I prefer the former approach but unsure about it's implementation.

Has anybody achieve this before and how did you implement?

  • 写回答

1条回答 默认 最新

  • dongxili9934 2016-07-26 19:54
    关注

    Take a look at my https://github.com/tideland/gocells. It's a library for event driven applications. Here so called cells are started with behaviors responsible to process the events, these cells are connected.

    Maybe you can use it or get some inspiration.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示