dr200166 2018-12-05 18:01
浏览 116
已采纳

Golang Docker API:获取事件

I want to get all new events from docker via the golang integration. The problem is that it returns two channels and I couldn't figure out how to subscribe to them.

cli, err := client.NewClientWithOpts(client.WithVersion("1.37"))
if err != nil {
    panic(err)
}

ctx, _ := context.WithCancel(context.Background())

msg, err := <- cli.Events(ctx, types.EventsOptions{})
  • 写回答

1条回答 默认 最新

  • dongshenyu4638 2018-12-05 21:45
    关注

    There are many solutions. A solution could be:

    msgs, errs := cli.Events(ctx, types.EventsOptions{})
    
    for {
            select {
              case err := <-errs:print(err)
              case msg := <-msgs:print(msg)
            }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试