Golang: How would I write a func that opens and allows a user to edit a text file, then continues running

huangapple go评论65阅读模式
英文:

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
}

huangapple
  • 本文由 发表于 2014年8月7日 21:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/25184354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定