dougu3290 2015-07-31 02:11
浏览 409
已采纳

使用golang运行命令行?

I'm just playing around with golang. I'm curious How could I run a gulpfile task from go?

Gulp task that is run from terminal typical:

gulp serv.dev

How could I run this simple line of code from golang:

package main
import (
    "net/http"
    "github.com/julienschmidt/httprouter"
    "fmt"
)

func main() {
    //what do I put here to open terminal in background and run `gulp serv.dev`
}
  • 写回答

3条回答 默认 最新

  • dsb238100 2015-07-31 02:15
    关注

    What you're looking for is exec.Command

    You'll pretty much want to spawn off a process that will run your gulp task.

    This can be done like so:

    package main
    
    import (
        "os/exec"
    )
    
    func main() {
        cmd := exec.Command("gulp", "serv.dev")
        if err := cmd.Run(); err != nil {
            log.Fatal(err)
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?