无法在 go 代码中调用 `vim`

我尝试vim在 go 程序中调用,其代码类似于:


package main


import (

        "fmt"

        "os"

        "os/exec"

)


func main() {

        err := exec.Command("vim", "a.txt").Run()

        if err != nil {

                fmt.Println(err)

        }

        os.Exit(0)

}

我跑go run mycode.go然后得到:


exit status 1

我尝试了几种方法来成功,例如替换Run()为Start(), Output(), ...,但似乎不起作用。最后,我尝试做的是尝试调用vim并停止我当前的 go 程序。我只想看到vim出现,仅此而已。


侃侃无极
浏览 126回答 1
1回答

蓝山帝景

为了让 vim 呈现其界面,您需要将标准输入/输出流附加到进程中:package mainimport (&nbsp; &nbsp; &nbsp; &nbsp; "fmt"&nbsp; &nbsp; &nbsp; &nbsp; "os"&nbsp; &nbsp; &nbsp; &nbsp; "os/exec")func main() {&nbsp; &nbsp; &nbsp; &nbsp; cmd := exec.Command("vim", "a.txt")&nbsp; &nbsp; &nbsp; &nbsp; cmd.Stdin = os.Stdin&nbsp; &nbsp; &nbsp; &nbsp; cmd.Stdout = os.Stdout&nbsp; &nbsp; &nbsp; &nbsp; cmd.Stderr = os.Stderr&nbsp; &nbsp; &nbsp; &nbsp; err := cmd.Run()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; os.Exit(0)}不附加流类似于从 shell 运行以下命令:vim < /dev/null > /dev/null 2> /dev/null
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go