doujupa7567 2017-07-26 18:57
浏览 37
已采纳

如何使用go例程和term ui从go代码退出

I recently started learning go and I am really impressed with all the features. I been playing with go routines and term-ui and facing some trouble. I am trying to exit this code from console after I run it but it just doesn't respond. If I run it without go-routine it does respond to my q key press event.

Any help is appreciated.

My code

package main

import (
    "fmt"
    "github.com/gizak/termui"
    "time"
    "strconv"
)

func getData(ch chan string) {
  i := 0
  for {
    ch <- strconv.Itoa(i)
    i++
    time.Sleep(time.Second)
    if i == 20 {
        break
    }
  }
}

func Display(ch chan string) {
  err := termui.Init()
  if err != nil {
    panic(err)
  }
  defer termui.Close()
  termui.Handle("/sys/kbd/q", func(termui.Event) {
    fmt.Println("q captured")
    termui.Close()
    termui.StopLoop()
  })

  for elem := range ch {
    par := termui.NewPar(elem)
    par.Height = 5
    par.Width = 37
    par.Y = 4
    par.BorderLabel = "term ui example with chan"
    par.BorderFg = termui.ColorYellow
    termui.Render(par)
  }
}

func main() {
  ch := make(chan string)
  go getData(ch)
  Display(ch)
}
  • 写回答

1条回答 默认 最新

  • douao7937 2017-07-26 20:59
    关注

    This is possibly the answer you are looking for. First off, you aren't using termui correctly. You need to call it's Loop function to start the Event loop so that it can actually start listening for the q key. Loop is called last because it essentially takes control of the main goroutine from then on until StopLoop is called and it quits.

    In order to stop the goroutines, it is common to have a "stop" channel. Usually it is a chan struct{} to save memory because you don't ever have to put anything in it. Wherever you want the goroutine to possibly stop and shutoff (or do something else perhaps), you use a select statement with the channels you are using. This select is ordered, so it will take from them in order unless they block, in which case it tries the next one, so the stop channel usually goes first. The stop channel normally blocks, but to get it to take this path, simply close()ing it will cause this path to be chosen in the select. So we close() it in the q keyboard handler.

    package main
    
    import (
        "fmt"
        "github.com/gizak/termui"
        "strconv"
        "time"
    )
    
    func getData(ch chan string, stop chan struct{}) {
        i := 0
        for {
            select {
            case <-stop:
                break
            case ch <- strconv.Itoa(i):
            }
            i++
            time.Sleep(time.Second)
            if i == 20 {
                break
            }
        }
    }
    
    func Display(ch chan string, stop chan struct{}) {
    
        for {
            var elem string
            select {
            case <-stop:
                break
            case elem = <-ch:
            }
            par := termui.NewPar(elem)
            par.Height = 5
            par.Width = 37
            par.Y = 4
            par.BorderLabel = "term ui example with chan"
            par.BorderFg = termui.ColorYellow
            termui.Render(par)
        }
    }
    
    func main() {
        ch := make(chan string)
        stop := make(chan struct{})
        err := termui.Init()
        if err != nil {
            panic(err)
        }
        defer termui.Close()
        termui.Handle("/sys/kbd/q", func(termui.Event) {
            fmt.Println("q captured")
            close(stop)
            termui.StopLoop()
        })
        go getData(ch, stop)
        go Display(ch, stop)
        termui.Loop()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么