The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
SuperFashi

Go 中是否有类 system() 函数

  •  
  •   SuperFashi ·
    superfashi · Sep 4, 2016 · 2959 views
    This topic created in 3565 days ago, the information mentioned may be changed or developed.

    今天搜了一天 Google ,啥都没找到……

    exec.Command 只能运行,并不能交互(很麻烦),而 syscall.Exec 则在子进程运行完后直接退出了。

    有没有类似于 Python 里的 os.system(),也就是 C/C++ 里的 system(),可以让我直接运行某个需要交互的程序(例如 vim 或者 git )并可以与其交互且退出后原程序能够继续运行?

    8 replies    2016-09-09 18:26:00 +08:00
    Phant0m
        1
    Phant0m  
       Sep 4, 2016   ❤️ 1
    可以参考下 gotty
    qiukun
        3
    qiukun  
       Sep 4, 2016   ❤️ 1
    @gxustudent good job
    raysonx
        4
    raysonx  
       Sep 4, 2016 via iPad   ❤️ 1
    system=fork+exec+waitpid
    SuperFashi
        5
    SuperFashi  
    OP
       Sep 4, 2016
    @gxustudent
    @raysonx
    感谢各位,用 cgo 解决问题了,看来 go 还是不太成熟……
    reus
        6
    reus  
       Sep 5, 2016   ❤️ 12
    不是 go 不成熟,是你技术不成熟而已,你并不理解“交互”是怎样实现的。
    如果你理解了,自然会这样做:

    package main

    import (
    "os"
    "os/exec"
    )

    func main() {
    cmd := exec.Command("vim")
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.Stdin = os.Stdin
    cmd.Run()
    }

    就这么简单。
    struCoder
        7
    struCoder  
       Sep 5, 2016
    @reus cool , 赞这句话
    SuperFashi
        8
    SuperFashi  
    OP
       Sep 9, 2016
    @reus
    哇,这个方法好多了,感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5447 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 09:03 · PVG 17:03 · LAX 02:03 · JFK 05:03
    ♥ Do have faith in what you're doing.