Search code examples
command-linegoterminalgulp

How to run a CLI command from Go?


How do I run a Gulp task from a Go program?

This is the command I run from a typical terminal:

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`?
}

Solution

  • 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)
        }
    }