Go 在wg.wait()完成后停止请求输入的选项
1。我触发了一个goroutine(运行第三方程序),我正在使用Go 在wg.wait()完成后停止请求输入的选项,go,Go,1。我触发了一个goroutine(运行第三方程序),我正在使用wg.Wait()等待它完成 2。在wg.Wait()之前,我想为用户提供一个选项来取消正在运行的第三方程序(如果他愿意的话) 3。第三方程序执行完成后,此用户输入选项应消失(没有理由停止已完成的进程)。目前,必须在触发wg.Wait()之前提供此输入 我怎么做?我想在goroutine中保留optionstop()函数,然后在wg.Wait()完成后将其杀死,但我无法完成,或者在我从XYZ返回之前,是否有方法向scanf的阻塞调用
wg.Wait()
等待它完成
2。在wg.Wait()
之前,我想为用户提供一个选项来取消正在运行的第三方程序(如果他愿意的话)
3。第三方程序执行完成后,此用户输入选项应消失(没有理由停止已完成的进程)。目前,必须在触发wg.Wait()
之前提供此输入
我怎么做?我想在goroutine中保留optionstop()
函数,然后在wg.Wait()
完成后将其杀死,但我无法完成,或者在我从XYZ返回之前,是否有方法向scanf的阻塞调用发送随机值?或者其他解决办法
更多详情:
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
}
func optiontoStop() {
var option string
fmt.Println("Type 'quit' if you want to quit the program")
fmt.Scanf("%s",&string)
//kill the process etc.
}
3
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
}
func optiontoStop() {
var option string
fmt.Println("Type 'quit' if you want to quit the program")
fmt.Scanf("%s",&string)
//kill the process etc.
}
您必须在另一个Go例程中处理您的用户输入,然后可能只需使用select,而不是
wg.Wait()
:
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
}
}
func ABC(){
完成:=make(chan结构{})
go func(){
延迟关闭(完成)
doSomething()
}()
停止:=make(chan结构{})
go func(){
延迟关闭(停止)
停止您不能使用Println Scanf进行此类操作。您不能轻易擦除Println写入终端的内容,也不能中断Scanf中stdin的读取。