无限循环 – 按任意键退出

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

Infinite loop - press any key to exit

问题

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

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

  1. func main() {
  2. for {
  3. doAll()
  4. }
  5. }

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

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

  1. func main() {
  2. for {
  3. doAll()
  4. }
  5. }
英文:

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?

  1. func main() {
  2. for {
  3. doAll()
  4. }
  5. }

答案1

得分: 7

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

  1. func main() {
  2. go func() {
  3. for {
  4. doAll()
  5. }
  6. }()
  7. fmt.Println("按任意键退出")
  8. var input string
  9. fmt.Scanf(input, "%s")
  10. }
英文:

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

  1. func main() {
  2. go func() {
  3. for {
  4. doAll()
  5. }
  6. }()
  7. fmt.Println("Press any key to exit")
  8. var input string
  9. fmt.Scanf(input, "%s")
  10. }

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:

确定