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

Go 中是否有类 system() 函数

  •  
  •   SuperFashi ·
    superfashi · Sep 4, 2016 · 3349 views
    This topic created in 3724 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
    Phant0m
    Sep 4, 2016❤️1
    1
    可以参考下 gotty
    qiukun
    qiukun
    Sep 4, 2016❤️1
    3
    @gxustudent good job
    raysonx
    raysonx
    Sep 4, 2016 via iPad❤️1
    4
    system=fork+exec+waitpid
    SuperFashi
    SuperFashi
    OP
    Sep 4, 2016
    5
    @gxustudent
    @raysonx
    感谢各位,用 cgo 解决问题了,看来 go 还是不太成熟……
    reus
    reus
    Sep 5, 2016❤️12
    6
    不是 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
    struCoder
    Sep 5, 2016
    7
    @reus cool , 赞这句话
    SuperFashi
    SuperFashi
    OP
    Sep 9, 2016
    8
    @reus
    哇,这个方法好多了,感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1916 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 11:33 · PVG 19:33 · LAX 03:33 · JFK 06:33
    ♥ Do have faith in what you're doing.