无限循环 – 按任意键退出

huangapple go评论83阅读模式
英文:

Infinite loop - press any key to exit

问题

我在我的应用程序中遇到了无限循环,并且我需要添加一个额外的功能。类似于“按任意键退出...”。

这是我的代码。谁知道一个好主意?

func main() {
   for {
      doAll()
   }
}

(翻译结果):
我在我的应用程序中遇到了无限循环,并且我需要添加一个额外的功能。类似于“按任意键退出...”。

这是我的代码。谁知道一个好主意?

func main() {
   for {
      doAll()
   }
}
英文:

I've infinite loop in my app and I need add one more functionality. Something like, "Press any key to exit...".

It's my code. Who knows the golden idea?

func main() {
   for {
      doAll()
   }
}

答案1

得分: 7

一种选择是在一个 goroutine 中启动 doAll 并调用 fmt.Scanf:

func main() {
    go func() {
        for {
            doAll()
        }
    }()

    fmt.Println("按任意键退出")
    var input string
    fmt.Scanf(input, "%s")
}
英文:

One option would be to start doAll in a goroutine and call fmt.Scanf:

func main() {
    go func() {
        for {
          doAll()
       }
    }()
    
    fmt.Println("Press any key to exit")
    var input string
    fmt.Scanf(input, "%s")
}

huangapple
  • 本文由 发表于 2014年10月13日 17:53:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/26337166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定