英文:
Golang: How would I write a func that opens and allows a user to edit a text file, then continues running
问题
我正在编写一个程序,它可以打开一个 .txt 文件,并允许用户编辑该文件,然后保存。我不太确定如何编写一个函数,在程序运行到一半时打开一个文本编辑器(如TextEdit、Cat、VIM等),等待用户对文件进行更改,然后在更改完成后继续运行。Go语言是否支持这样做?如果有任何建议或示例,将不胜感激。
英文:
I'm in the process of writing a program that will open a .txt file, and allow the user to edit the file, then save it. I'm not really sure how to go about writing a func that opens a text editor(TextEdit, Cat, VIM, w/e) halfway through the program, waits for a user to make changes to that file, then continues running once those changes are complete. Is go capable of doing this? Any suggestions/examples would be appreciated.
答案1
得分: 4
这实际上与Go语言没有太大关系,你只需要启动进程,等待其退出,然后进行你的操作:
cmd := exec.Command("vim", "file.txt")
if cmd.Run() != nil {
// vim没有以状态码0退出
} else {
// 运行成功,对file.txt进行操作
}
英文:
This really doesn't have anything to do with go specifically, you start the process, wait for it to exit and then do your thing:
cmd := exec.Command("vim", "file.txt")
if cmd.Run() != nil {
//vim didn't exit with status code 0
} else {
//it worked, do stuff with file.txt
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论