如何在 Go 中编写一个程序,在编辑器中打开文件并在退出时恢复该程序?
我需要在 Go 中编写一个函数,在 Vim 或 Nano 中打开文件,然后在退出编辑器时恢复 Go 程序。
这就是我所拥有的:
func OpenEd(page string) error {
pagePath := GetPath(page, DEBUG)
ed := "/usr/bin/nano"
cmd := exec.Command(ed, pagePath)
err := cmd.Start()
cmd.Wait()
return err
}
它运行时没有错误,但什么也不做。我认为我必须分叉主进程才能正确运行 exec 功能,就像 C 中典型的那样。但我对此进行了研究,我认为 Go 中不是这样处理的。但我不确定,Go 对我来说相当陌生。
C 中的这段代码完全按照我希望 Go 代码运行的方式运行:
void openEd(void)
{
int rc;
char *args[] = {"test.txt", NULL};
rc = fork();
if (rc == 0) {
execvp("nano", args);
}
wait(&rc);
}
How do I get this Go function to run like this C function?
I need to write a function in Go that opens a file in Vim or Nano and then upon exiting the editor, have the Go program resume.
Here's what I have:
func OpenEd(page string) error {
pagePath := GetPath(page, DEBUG)
ed := "/usr/bin/nano"
cmd := exec.Command(ed, pagePath)
err := cmd.Start()
cmd.Wait()
return err
}
It runs with no error but does nothing. I was thinking that I would have had to fork the main process to run the exec functionality properly like what is typical in C. But I looked into that and I don't think that's how it's handled in Go. But I'm not sure, Go is fairly new to me.
This code in C runs exactly how I want the Go code to run:
void openEd(void)
{
int rc;
char *args[] = {"test.txt", NULL};
rc = fork();
if (rc == 0) {
execvp("nano", args);
}
wait(&rc);
}
How do I get this Go function to run like this C function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论