Ability to stop prompting for input after wg.wait () is executed

1 . I called goroutine (which launches a third-party program) and I use wg.Wait()to wait for completion

2 . Before wg.Wait(), I want to give the user the opportunity to cancel this third-party program that works (if he wants)

3 . After the third party is executed, the execution of this user input option should disappear (there is no reason why it should stop the process that has already been executed). Currently, this input must be provided before triggering.wg.Wait()

How can i do this? I was thinking about saving the function optiontoStop()in goroutine and then killing it after completion wg.Wait(), but I could not execute it, or is there a way to send a random value to the scanf blocking call before I return from XYZ? or any other workarounds?

More details:

1 .

func XYZ() {
   wg.Add(1)
   go doSomething(&wg) // this runs a third party program
}

2 .

func ABC() {
   XYZ()
   optiontoStop() // I want this input wait request to vanish off after 
                  // the third party program execution                    
                  // (doSomething()) is completed
   wg.Wait() 
   //some other stuff 
}

3 .

func optiontoStop() {
   var option string
   fmt.Println("Type 'quit'  if you want to quit the program")
   fmt.Scanf("%s",&string)
   //kill the process etc.
 }
+4
source share
1 answer

You should handle your user input in another Go procedure, but instead wg.Wait(), maybe just use select:

func ABC() {
    done := make(chan struct{})
    go func() {
        defer close(done)
        doSomething()
    }()
    stop := make(chan struct{})
    go func() {
        defer close(stop)
        stop <- optionToStop()
    }
    select {
    case done:
        // Finished, close optionToStop dialog, and move on
    case stop:
        // User requested stop, terminate the 3rd party thing and move on
    }
}
+3
source

Source: https://habr.com/ru/post/1675204/


All Articles