Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a command line using 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`
}
like image 713
Armeen Harwood Avatar asked Jul 31 '15 02:07

Armeen Harwood


People also ask

How do you pass command-line arguments in Golang?

To access all command-line arguments in their raw format, we need to use Args variables imported from the os package . This type of variable is a string ( [] ) slice. Args is an argument that starts with the name of the program in the command-line. The first value in the Args slice is the name of our program, while os.

What is go command in Golang?

The Go distribution includes a command, named " go ", that automates the downloading, building, installation, and testing of Go packages and commands.


1 Answers

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)
    }
}
like image 175
Leo Correa Avatar answered Oct 01 '22 19:10

Leo Correa