JakubKubera
JakubKubera

Reputation: 436

Infinite loop - press any key to exit

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()
   }
}

Upvotes: 2

Views: 2316

Answers (1)

alexn
alexn

Reputation: 59002

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")
}

Upvotes: 7

Related Questions