此代码选择同一文件夹中的所有xml文件,与调用的可执行文件一样,并且异步地将处理应用于回调方法中的每个结果(在下面的示例中,只打印出文件的名称).
如何避免使用sleep方法来保持main方法退出?我有问题缠绕我的头围绕通道(我认为这是需要的,同步结果)所以任何帮助表示赞赏!
package main
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"os"
"runtime"
"time"
)
func eachFile(extension string, callback func(file string)) {
exeDir := filepath.Dir(os.Args[0])
files, _ := ioutil.ReadDir(exeDir)
for _, f := range files {
fileName := f.Name()
if extension == path.Ext(fileName) {
go callback(fileName)
}
}
}
func main() {
maxProcs := runtime.NumCPU()
runtime.GOMAXPROCS(maxProcs)
eachFile(".xml", func(fileName string) {
// Custom logic goes in here
fmt.Println(fileName)
})
// This is what i want to get rid of
time.Sleep(100 * time.Millisecond) …Run Code Online (Sandbox Code Playgroud) 我在ubuntu上.当我在VS Code中编写C++代码时,它会自动发送类似的内容
if (condition == true)
{
DoStuff();
}
Run Code Online (Sandbox Code Playgroud)
相反,我想做
if (condition == true) {
DoStuff();
}
Run Code Online (Sandbox Code Playgroud)
我怎么做?我已经从市场上安装了C/C++扩展.
在大多数现代shell中,您可以点击向上和向下箭头,它会在提示符下放置您执行的先前命令.我的问题是,这是如何工作的?!
在我看来,shell以某种方式操纵stdout来覆盖它已经编写的内容?
我注意到像wget这样的程序也可以这样做.有人知道他们是怎么做到的吗?