1. I triggered a goroutine (which runs a third party program) and I am using wg.Wait()
to wait for it to complete
2. Before wg.Wait()
, I want to provide user an option to cancel this third party program that is running (if he want to)
3. After the third party program execution is done, this user input option should vanish off (there is no reason why he should stop the process thats already done). Currently this input has to be provided before wg.Wait()
is triggered
How can I do it? I thought of keeping the optiontoStop()
function in goroutine and then kill it after wg.Wait()
is done but I wasn't able to get it done or else is there a way to send a random value to the blocking call of scanf 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.
}
You have to handle your user input in another Go routine, then instead of wg.Wait()
, probably just use a 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
}
}